简体   繁体   English

使用RequireJS敲除自定义引导程序绑定错误

[英]Knockout custom bootstrap binding error with RequireJS

I have a custom knockout binding for bootstrap tooltips and some times the page loads fine and some times I get this error being thrown in the binding. 我有一个针对bootstrap工具提示的自定义knockout绑定,有时候页面加载很好,有时我在绑定中抛出这个错误。

$(...).tooltip is not a function $(...)。tooltip不是一个函数

I have set a break point in the section where this is happening and sometimes the jquery object doesn't have any bootstrap functions on it. 我在发生这种情况的部分设置了一个断点,有时jquery对象上没有任何引导函数。 I can only assume that this is an issue with the timing of when the bootstrap library is loaded by require. 我只能假设这是由require加载引导程序库的时间问题。 Like I said, it doesn't happen every time and it seems to happen much more often when I have the dev tools console open. 就像我说的那样,每次都不会发生这种情况,而且当开启dev工具控制台时,它似乎经常发生。

Here is my require config 这是我的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'
    }
});

And my binding 而我的约束力

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'
        }
    };
});

My apply bindings 我的申请绑定

define(['jquery', 'bootstrap', 'appVM', 'knockout', 'domReady!'], 
    function(jquery, bootstrap, appVM, ko) {
    var vm = new appVM();
    ko.applyBindings(vm);
});

Add your customBindings to the dependencies list in the module which calls ko.applyBindings 您添加customBindings的依赖关系列表,它调用模块中ko.applyBindings

define(['jquery', 'bootstrap', 'appVM', 'knockout', 'customBindings', 'domReady!'],
    function(jquery, bootstrap, appVM, ko) {
    var vm = new appVM();
    ko.applyBindings(vm);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM