如何扩展 Twitter Bootstrap 插件
我正在深入研究 Twitter 的 Bootstrap,现在想尝试向插件添加一些功能,但我不知道该怎么做。使用模态插件作为示例(http://twitter.github.com/bootstrap/javascript.html#modals< /a>),我想向插件添加一个新函数,可以像标准插件方法一样调用它。我认为最接近的是下面的代码,但是当我尝试访问时,我得到的只是该函数不是对象的一部分。
有什么建议吗?这是我尝试过的,这似乎是最接近我需要做的:
$.extend($.fn.modal, {
showFooterMessage: function (message) {
alert("Hey");
}
});
然后我想按如下方式调用它:
$(this).closest(".modal").modal("showFooterMessage");
编辑:好的,我想出了如何做到这一点:
(function ($) {
var extensionMethods = {
displayFooterMessage: function ($msg) {
var args = arguments[0]
var that = this;
// do stuff here
}
}
$.extend(true, $.fn.modal.Constructor.prototype, extensionMethods);
})(jQuery);
现有的 Bootstrap 插件集的问题问题是,如果有人想扩展它们,则所有新方法都不能接受参数。我尝试“修复”这个问题是在插件函数调用中添加对参数的接受。
$.fn.modal = function (option) {
var args = arguments[1] || {};
return this.each(function () {
var $this = $(this)
, data = $this.data('modal')
, options = typeof option == 'object' && option
if (!data) $this.data('modal', (data = new Modal(this, options)))
if (typeof option == 'string') data[option](args)
else data.show()
}) // end each
} // end $.fn.modal
I am digging into Twitter's Bootstrap and now want to try and add some functionality to the plugins, but I can't figure out how to do so. Using the modal plugin as an example (http://twitter.github.com/bootstrap/javascript.html#modals), I'd like to add a new function to the plugin that can be called as one would the standard plugin methods. THe closest I think I have come is with the following code, but all I get when I try to access is that the function is not part of the object.
Any suggestions? This is what I have tried that seems to be the closest to what I need to do:
$.extend($.fn.modal, {
showFooterMessage: function (message) {
alert("Hey");
}
});
Then I would want to call it as follows:
$(this).closest(".modal").modal("showFooterMessage");
EDIT: OK, I figured out how to do this:
(function ($) {
var extensionMethods = {
displayFooterMessage: function ($msg) {
var args = arguments[0]
var that = this;
// do stuff here
}
}
$.extend(true, $.fn.modal.Constructor.prototype, extensionMethods);
})(jQuery);
The problem with the existing set of Bootstrap plugins is that if anyone wants to extend them, none of the new methods can accept arguments. My attempt to "fix" this was to add the acceptance of arguments in the plugins function call.
$.fn.modal = function (option) {
var args = arguments[1] || {};
return this.each(function () {
var $this = $(this)
, data = $this.data('modal')
, options = typeof option == 'object' && option
if (!data) $this.data('modal', (data = new Modal(this, options)))
if (typeof option == 'string') data[option](args)
else data.show()
}) // end each
} // end $.fn.modal
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个旧线程,但我只是使用以下模式对模态进行了一些自定义扩展:
这样您可以
1) 添加自己的默认选项
2) 使用自定义参数创建新方法并访问原始(超级)函数
3) do在调用原始构造函数之前和/或之后构造函数中的内容
This is an old thread, but I just made some custom extensions to modal using the following pattern:
This way you can
1) add your own default options
2) create new methods with custom arguments and access to original (super) functions
3) do stuff in the constructor before and/or after the original constructor is called
郑重声明,有一个简单的方法可以覆盖或扩展现有函数:
不涵盖自定义参数,但使用上面的方法也很容易;不要使用
apply
和arguments
,而是在函数定义中指定原始参数和自定义参数,然后调用原始_show
(或任何一个)是)使用原始参数,最后使用新参数做其他事情。For the record there is a simple method to override or extend existing functions:
Doesn't cover custom arguments, but it's easy as well with the method above; instead of using
apply
andarguments
, specify the original arguments plus the custom ones in the function definition, then call the original_show
(or whichever it is) with the original arguments, and finally do other stuff with the new arguments.作为参考,我遇到了类似的问题,因此我通过分叉和“扩展”了 Typeahead 插件在插件脚本本身中添加我需要的功能。
如果您必须更改源代码,那么它实际上并不是一个扩展,因此我认为将所有修改放在一个地方会更容易。
For reference, I had a similar problem so I "extended" the Typeahead plugin by forking and adding the functionality I needed in the plugin script itself.
If you have to change the source it's not really an extension anyways so I figured it was easier just to put all my modifications in one place.
对于任何希望使用 Bootstrap 3 执行此操作的人。插件布局已发生一些更改,所有 Bootstrap 3 插件的默认选项都附加到插件构造函数中:
For anyone looking to do this with Bootstrap 3. The plugin layout has changed a bit the default options for all Bootstrap 3 plugins are attached to the plugins constructor:
+1 以上来自大卫接受的答案。但如果我可以进一步简化它,我会调整
DEFAULTS
扩展和$fn.modal
扩展,如下所示:+1 for the accepted answer above from David. But if I could simplify it a bit more, I'd adjust the
DEFAULTS
extending and$fn.modal
extending like so: