我有一个用于 Bootstrap 工具提示的自定义 knockout 绑定(bind),有时页面加载正常,有时我在绑定(bind)中抛出此错误。
$(...).tooltip is not a function
我在发生这种情况的部分设置了一个断点,有时 jquery 对象没有任何 Bootstrap 函数。我只能假设这是 Bootstrap 库被 require 加载的时间问题。就像我说的,它不会每次都发生,当我打开开发工具控制台时,它似乎更频繁地发生。
这是我的需求配置
requirejs.config({
waitSeconds: 200,
shim: {
"bootstrap": {
deps: ['jquery'],
exports: "$.fn.tooltip"
}
},
enforceDefine: true,
paths: {
jquery: 'jquery-1.11.3.min',
bootstrap: 'bootstrap.min',
moment: 'moment.min',
knockout: 'knockout.min',
appVM: 'appVM',
custombindings: 'custombindings'
}
});
还有我的绑定(bind)
define(['knockout', 'jquery', 'bootstrap'], function (ko, $, bootstrap) {
ko.bindingHandlers.tooltip = {
init: function (element, valueAccessor) {
var local = ko.utils.unwrapObservable(valueAccessor()),
options = {};
ko.utils.extend(options, ko.bindingHandlers.tooltip.options);
ko.utils.extend(options, local);
$(element).tooltip(options); //Error thrown here
ko.utils.domNodeDisposal.addDisposeCallback(element, function () {
$(element).tooltip("destroy");
});
},
options: {
'container': 'body',
'delay': { 'show': 1000, 'hide': 100 },
'animation': 'true'
}
};
});
我的应用绑定(bind)
define(['jquery', 'bootstrap', 'appVM', 'knockout', 'domReady!'],
function(jquery, bootstrap, appVM, ko) {
var vm = new appVM();
ko.applyBindings(vm);
});
最佳答案
将您的 customBindings
添加到调用 ko.applyBindings
的模块的依赖项列表中
define(['jquery', 'bootstrap', 'appVM', 'knockout', 'customBindings', 'domReady!'],
function(jquery, bootstrap, appVM, ko) {
var vm = new appVM();
ko.applyBindings(vm);
});
关于javascript - 使用 RequireJS knockout 自定义 Bootstrap 绑定(bind)错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34340898/