【发布时间】:2013-06-20 19:09:06
【问题描述】:
是否可以覆盖缩小的 Javascript 文件中包含的函数?
详情: 我正在尝试覆盖 Twitter Bootstrap Typeahead 插件 (v2.3.2) 中的 process() 函数,以便在返回的项目超过显示的项目时在下拉列表底部添加一个指示器。
这是我的代码:
var customProcess = function (items) {
var that = this
items = $.grep(items, function (item) {
return that.matcher(item)
})
items = this.sorter(items)
if (!items.length) {
return this.shown ? this.hide() : this
}
//Get the default item slice and determine whether the indicator is needed
var itemSlice = items.slice(0, this.options.items);
if (items.length > this.options.items) {
itemSlice.push("...");
}
return this.render(itemSlice).show();
};
// Reassign the typeahead.process() function to the customized
// version that adds a visual indicator if more items are
// returned than the amount shown (options.items).
$.fn.typeahead.Constructor.prototype.process = customProcess;
如果我使用的是 Bootstrap JavaScript (bootstrap.min.js) 的缩小版本,这将失败并且实际上完全终止了 typeahead 功能。如果我改为获取非缩小版本(bootstrap.js),它会完美运行。
[附带说明一下,我之前在较早版本的 typeahead 插件(我相信是 v1.8.x)中使用了相同的方法,并且它也可以与最小化版本完美配合。我是不是走运了?]
【问题讨论】:
-
尝试使用像 jscompress.com 这样的压缩工具来缩小非缩小版本(对你有用)并检查它是否有效。
-
@ParthikGosar jscompress.com 不起作用,但我在 refresh-sf.com/yui 找到了另一个。使用新压缩的文件,覆盖成功。至于我最初的问题,只要缩小的 JS 是正确的,覆盖缩小的 JS 似乎是完全有效的。我对吗?感谢您的帮助!
-
是的,你是对的......缩小 JS 文件时的常见错误之一是我们有时会错过在代码中添加分号。因此,当文件被缩小时,连续的行被计为同一行(因为这些行之间没有分号)。有些缩小器在必要时添加分号,有些则没有。这种情况会在缩小文件中引入错误。
标签: javascript twitter-bootstrap bootstrap-typeahead