Skip to content

Commit 640e352

Browse files
author
Eonasdan
committed
Merge pull request Eonasdan#1458 from neocotic/clean-method-invocation
Clean method invocation
2 parents a6f7df9 + c2964e5 commit 640e352

File tree

2 files changed

+481
-13
lines changed

2 files changed

+481
-13
lines changed

src/js/bootstrap-datetimepicker.js

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2101,6 +2101,10 @@
21012101
};
21022102

21032103
picker.keyBinds = function (keyBinds) {
2104+
if (arguments.length === 0) {
2105+
return options.keyBinds;
2106+
}
2107+
21042108
options.keyBinds = keyBinds;
21052109
return picker;
21062110
};
@@ -2373,14 +2377,42 @@
23732377
********************************************************************************/
23742378

23752379
$.fn.datetimepicker = function (options) {
2376-
return this.each(function () {
2377-
var $this = $(this);
2378-
if (!$this.data('DateTimePicker')) {
2379-
// create a private copy of the defaults object
2380-
options = $.extend(true, {}, $.fn.datetimepicker.defaults, options);
2381-
$this.data('DateTimePicker', dateTimePicker($this, options));
2382-
}
2383-
});
2380+
options = options || {};
2381+
2382+
var args = Array.prototype.slice.call(arguments, 1),
2383+
isInstance = true,
2384+
thisMethods = ['destroy', 'hide', 'show', 'toggle'],
2385+
returnValue;
2386+
2387+
if (typeof options === 'object') {
2388+
return this.each(function () {
2389+
var $this = $(this);
2390+
if (!$this.data('DateTimePicker')) {
2391+
// create a private copy of the defaults object
2392+
options = $.extend(true, {}, $.fn.datetimepicker.defaults, options);
2393+
$this.data('DateTimePicker', dateTimePicker($this, options));
2394+
}
2395+
});
2396+
} else if (typeof options === 'string') {
2397+
this.each(function () {
2398+
var $this = $(this),
2399+
instance = $this.data('DateTimePicker');
2400+
if (!instance) {
2401+
throw new Error('bootstrap-datetimepicker("' + options + '") method was called on an element that is not using DateTimePicker');
2402+
}
2403+
2404+
returnValue = instance[options].apply(instance, args);
2405+
isInstance = returnValue === instance;
2406+
});
2407+
2408+
if (isInstance || $.inArray(options, thisMethods) > -1) {
2409+
return this;
2410+
}
2411+
2412+
return returnValue;
2413+
}
2414+
2415+
throw new TypeError('Invalid arguments for DateTimePicker: ' + options);
23842416
};
23852417

23862418
$.fn.datetimepicker.defaults = {

0 commit comments

Comments
 (0)