javascript - JS Closure - Multiple date picker initializations / invocations that relies on callback -
how invoke , initialize multiple datepickers (bootstrap date picker) on following script using closure not have duplicate functions each date picker id?
http://www.daterangepicker.com/ picker using
cb_helper <---- tried use closure unsuccessfully "remember" id passed in...
what doing wrong?
js
$(function() { var start = moment().subtract(29, 'days'); var end = moment(); var idval = ""; function cb(start, end) { $(idval + ' span').html(start.format('mmmm d, yyyy') + ' - ' + end.format('mmmm d, yyyy')); } function cb_helper(id, start, end) { (function(){ idval = id; cb(start, end); })(); } function init(id){ $(id).daterangepicker({ startdate: start, enddate: end, ranges: { 'today': [moment(), moment()], 'yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')], 'last 7 days': [moment().subtract(6, 'days'), moment()], 'last 30 days': [moment().subtract(29, 'days'), moment()], 'this month': [moment().startof('month'), moment().endof('month')], 'last month': [moment().subtract(1, 'month').startof('month'), moment().subtract(1, 'month').endof('month')] } }, cb); } init("#reportrange_profitability"); init("#reportrange_volume"); cb_helper("#reportrange_profitability", start, end); cb_helper("#reportrange_volume", start, end); });
$(function() { var start = moment().subtract(29, 'days'); var end = moment(); function cb(idval) { return function(start, end) { $(idval + ' span').html(start.format('mmmm d, yyyy') + ' - ' + end.format('mmmm d, yyyy')); } } function init(id, start, end) { $(id).daterangepicker({ startdate: start, enddate: end, ranges: { 'today': [moment(), moment()], 'yesterday': [moment().subtract(1, 'days'), moment().subtract(1, 'days')], 'last 7 days': [moment().subtract(6, 'days'), moment()], 'last 30 days': [moment().subtract(29, 'days'), moment()], 'this month': [moment().startof('month'), moment().endof('month')], 'last month': [moment().subtract(1, 'month').startof('month'), moment().subtract(1, 'month').endof('month')] } }, cb(id)); } init("#reportrange_profitability", start, end); init("#reportrange_volume", start, end); });
Comments
Post a Comment