javascript - How to override a Bootstrap plugin definition -
i want make adjustments (actually, fixes) bootstrap 3 button plugin, can't wait whole submit code/pull request/approval process. don't want make edits directly bootstrap.js file. thought add file called bootstrap-override.js loads after bootstrap.js , contained copy of buttons plugin code customizations, isn't working. code original buttons plugin fires.
what correct pattern replacing defined plugin this:
/* ======================================================================== * bootstrap: button.js v3.3.0 * http://getbootstrap.com/javascript/#buttons * ======================================================================== * copyright 2011-2014 twitter, inc. * licensed under mit (https://github.com/twbs/bootstrap/blob/master/license) * ======================================================================== */ +function ($) { 'use strict'; // button public class definition // ============================== var button = function (element, options) { this.$element = $(element) this.options = $.extend({}, button.defaults, options) this.isloading = false } button.version = '3.3.0' button.defaults = { loadingtext: 'loading...' } button.prototype.setstate = function (state) { var d = 'disabled' var $el = this.$element var val = $el.is('input') ? 'val' : 'html' var data = $el.data() state = state + 'text' if (data.resettext == null) $el.data('resettext', $el[val]()) // push event loop allow forms submit settimeout($.proxy(function () { $el[val](data[state] == null ? this.options[state] : data[state]) if (state == 'loadingtext') { this.isloading = true $el.addclass(d).attr(d, d) } else if (this.isloading) { this.isloading = false $el.removeclass(d).removeattr(d) } }, this), 0) } button.prototype.toggle = function () { var changed = true var $parent = this.$element.closest('[data-toggle="buttons"]') if ($parent.length) { var $input = this.$element.find('input') if ($input.prop('type') == 'radio') { if ($input.prop('checked') && this.$element.hasclass('active')) changed = false else $parent.find('.active').removeclass('active') } if (changed) $input.prop('checked', !this.$element.hasclass('active')).trigger('change') } else { this.$element.attr('aria-pressed', !this.$element.hasclass('active')) } if (changed) this.$element.toggleclass('active') } // button plugin definition // ======================== function plugin(option) { return this.each(function () { var $this = $(this) var data = $this.data('bs.button') var options = typeof option == 'object' && option if (!data) $this.data('bs.button', (data = new button(this, options))) if (option == 'toggle') data.toggle() else if (option) data.setstate(option) }) } var old = $.fn.button $.fn.button = plugin $.fn.button.constructor = button // button no conflict // ================== $.fn.button.noconflict = function () { $.fn.button = old return } // button data-api // =============== $(document) .on('click.bs.button.data-api', '[data-toggle^="button"]', function (e) { var $btn = $(e.target) if (!$btn.hasclass('btn')) $btn = $btn.closest('.btn') plugin.call($btn, 'toggle') e.preventdefault() }) .on('focus.bs.button.data-api blur.bs.button.data-api', '[data-toggle^="button"]', function (e) { $(e.target).closest('.btn').toggleclass('focus', e.type == 'focus') }) }(jquery);
you need remove original bootstrap button plugin's event handlers using jquery.off()
. need .off()
each of original button plugin's .on()
s. example:
$('document').off('click.bs.button.data-api focus.bs.button.data-api blur.bs.button.data-api');
Comments
Post a Comment