无法阅读属性'当前' of null select2.js w knockout

时间:2018-01-22 08:24:06

标签: javascript html knockout.js jquery-select2

我正在使用select2进入淘汰赛自定义绑定。直到几天前一切正常,但现在它停止工作给我这个未被捕获的类型错误。很多人都有同样的问题,但我无法弄清楚发生了什么

这是我的html绑定

<select class="select2" multiple id="domainSelector" data-bind="selectedOptions:subDomains, select2:{placeholder:i18n('SATA-CerDom:alternativeNames'),allowClear:true,tags:names(),closeOnSelect:false,formatSearching:i18n('SATA-CerDom:formatSearching')}" tabindex="-1"></select>

这是我的约束力

    ko.bindingHandlers.select2 = {
        init: function (el, valueAccessor, allBindingsAccessor, viewModel) {
            ko.utils.domNodeDisposal.addDisposeCallback(el, function () {
                $(el).select2('destroy');
            });

            const allBindings = allBindingsAccessor();
            const select2 = ko.utils.unwrapObservable(allBindings.select2);

            $(el).select2(select2);
            if ("selectedOptions" in allBindings)
                $(el).val(allBindings.selectedOptions()).trigger('change');
        },
        update: function (el, valueAccessor, allBindingsAccessor, viewModel) {
            const allBindings = allBindingsAccessor();

            if ("value" in allBindings && allBindings.value()) {
                if ((allBindings.select2.multiple || el.multiple) && allBindings.value().constructor !== Array) {
                    $(el).val(allBindings.value().split(',')).trigger('change');
                }
                else {
                    $(el).val(allBindings.value()).trigger('change');
                }
            } else if ("selectedOptions" in allBindings) {
                let textAccessor = v => v;
                if ("optionsText" in allBindings) {
                    textAccessor = (value) => {
                        let valueAccessor = i => i;
                        if ("optionsValue" in allBindings)
                            valueAccessor = item => item[allBindings.optionsValue];

                        let items = $.grep(allBindings.options(), e => valueAccessor(e) === value);
                        if (items.length === 0 || items.length > 1)
                            return "UNKNOWN";
                        return items[0][allBindings.optionsText];
                    }
                }
                const converted = [];
                $(el).select2("data", allBindings.selectedOptions().map(v => {
                    return {id: v, text: textAccessor(v)}
                }));
            }
            if("tags" in valueAccessor()){
                $(el).select2();
                const converted = [];
                $(el).select2(ko.unwrap(valueAccessor()));
            }
            $(el).trigger("change");
        }
    };

0 个答案:

没有答案