I create two js file for a task. I want to merge two file task in one file in three function. Both two have different config parameter. I am using magento x init.
<script type="text/x-magento-init">
{
"body" : {
"dealsdow":
{
"dowDealspids": <?php echo json_encode($arrDowDealsPids); ?>
}
}
}
</script>
Javscript file.
define([
"jquery",
"mage/url",
"jquery/ui",
"countdowntimer"
], function($,urlBuilder) {
"use strict";
var greatDeal = function {
$(document).ready(function () {
var today = new Date();
var dealEndDate = '';
var date = today.getFullYear()+'/'+(today.getMonth()+1)+'/'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var currentTime = date+' '+time;
var dealEndDate = currentTime;
var i = '';
var dealUrl = urlBuilder.build("catalog/timer/getdata");
for(i=0;i<config.dowDealspids.length; i++){
$.ajax({
type: "POST",
url: dealUrl,
data :{pid:config.dowDealspids[i]},
cache: false,
async:false,
success: function (result) {
if (result) {
dealEndDate = result;
}
},
failure: function (response) {
}
});
var now = currentTime;
var arrDealEndDate = dealEndDate.split('-');
var newDate = arrDealEndDate[1] + '/' + arrDealEndDate[0] + '/' + arrDealEndDate[2];
var end = new Date(arrDealEndDate[1] + '/' + arrDealEndDate[0] + '/' + arrDealEndDate[2]);
var id = config.dowDealspids[i];
id = "dow"+id;
$("#future_date_" + id).countdowntimer({
startDate: now,
dateAndTime: end,
size: "lg",
regexpMatchFormat: "([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})",
regexpReplaceWith: "$1 days, $2: $3: $4"
});
}
});
}
var weekDeal = function (config) {
//console.log(config); // will output {a: "Hello from template"}
$(document).ready(function () {
var today = new Date();
var dealEndDate = '';
var date = today.getFullYear()+'/'+(today.getMonth()+1)+'/'+today.getDate();
var time = today.getHours() + ":" + today.getMinutes() + ":" + today.getSeconds();
var currentTime = date+' '+time;
var dealEndDate = currentTime;
var i = '';
var greatDealUrl = urlBuilder.build("catalog/timer/getdata");
for(i=0;i<config.greatDealspids.length; i++){
$.ajax({
type: "POST",
url: greatDealUrl,
data :{pid:config.greatDealspids[i]},
cache: false,
async:false,
success: function (result) {
if (result) {
dealEndDate = result;
}
},
failure: function (response) {
}
});
var now = currentTime;
var arrDealEndDate = dealEndDate.split('-');
var newDate = arrDealEndDate[1] + '/' + arrDealEndDate[0] + '/' + arrDealEndDate[2];
var end = new Date(arrDealEndDate[1] + '/' + arrDealEndDate[0] + '/' + arrDealEndDate[2]);
var id = config.greatDealspids[i];
id = "gd"+id;
$("#future_date_" + id).countdowntimer({
startDate: now,
dateAndTime: end,
size: "lg",
regexpMatchFormat: "([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})",
regexpReplaceWith: "$1 days, $2: $3: $4"
});
}
});
}
return weekDeal;
return greatDeal;
});