Last active
August 29, 2015 13:55
-
-
Save kepek/8745439 to your computer and use it in GitHub Desktop.
How to extend Bootstrap JavaScript Plugin? See below :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+function ($) { | |
'use strict'; | |
// BOOTSTRAP | |
var Bootstrap = $.fn.modal.Constructor; | |
// MODAL CLASS DEFINITION | |
// ====================== | |
var Modal = function (element, options) { | |
this.options = options | |
this.$element = $(element) | |
this.$backdrop = | |
this.isShown = null | |
if (this.options.remote) { | |
this.$element | |
.find('.modal-content') | |
.load(this.options.remote, $.proxy(function () { | |
this.$element.trigger('loaded.bs.modal') | |
}, this)) | |
} | |
}; | |
Modal.DEFAULTS = { | |
backdrop: true, | |
keyboard: true, | |
show: true | |
} | |
Modal.prototype = $.extend(true, Bootstrap.prototype, {}); | |
Modal.prototype.toggle = function (_relatedTarget) { | |
/** | |
* Method body here. | |
*/ | |
console.log('Method body here.') | |
return this[!this.isShown ? 'show' : 'hide'](_relatedTarget) | |
} | |
// REMOVE OLD PLUGIN | |
// ======================= | |
delete $.fn.modal; | |
// MODAL PLUGIN DEFINITION | |
// ======================= | |
var old = $.fn.modal; | |
$.fn.modal = function (option, _relatedTarget) { | |
return this.each(function () { | |
var $this = $(this) | |
var data = $this.data('bs.modal') | |
var options = $.extend({}, Modal.DEFAULTS, $this.data(), typeof option == 'object' && option) | |
if (!data) $this.data('bs.modal', (data = new Modal(this, options))) | |
if (typeof option == 'string') data[option](_relatedTarget) | |
else if (options.show) data.show(_relatedTarget) | |
}) | |
} | |
$.fn.modal.Constructor = Modal; | |
// MODAL NO CONFLICT | |
// ================= | |
$.fn.modal.noConflict = function () { | |
$.fn.modal = old | |
return this | |
} | |
}(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment