如何订购敲除绑定? [英] How to order knockout bindings?

查看:76
本文介绍了如何订购敲除绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用淘汰表.js.我陷入了一些奇怪的情况(很难解释,但是我正在尝试,如果不清楚的话,对不起).我在单个选择列表上使用自定义绑定和选项绑定:

I am using knockout.js. I am stuck in a little bit strange situation ( its hard to explain but i am trying, sorry if i am not clear ). I am using custom binding and options binding on a single select-list :

  <select data-bind="options : arrayOfOptions, optionsText: 'Name', 
           optionsValue: 'Name', chosen: { }">
  </select>

ko.bindingHandlers.chosen = {
    init: function (element, valueAccessor, allBindingAccessor, 
                    viewModel, bindigContext) {
        var options = ko.utils.unwrapObservable(valueAccessor() || {});
        $(element).chosen(options);
    }
};

在运行时,选择列表将填充arrayOfOptions数组中的所有可用选项,并且chosen是自定义绑定,我在其中应用

Here at runtime selectlist will fill with all available options from the arrayOfOptions array and chosen is a custom binding in which i am applying a CHOSEN PLUGIN on the select-list.

现在我在这里面临的问题是,当我在选择列表上应用选择插件时,在自定义绑定中,选择列表未填充arrayOfOptions数组中的选项.用简单的术语表示custom bindingoptions binding之前执行.有人可以给我一个解决方案,以便在选项绑定之后应用自定义绑定吗?

Now the problem i am facing here is that in custom binding when i applied choose plugin on select list at that time the selectlist is not filled with the options from the arrayOfOptions array. Means in a simple term the custom binding is executing before options binding. Can anybody please give me a solution for this so that custom binding applied after options binding?

推荐答案

将对chosen的调用移动到更新中.

Move your call to chosen into the update.

http://jsfiddle.net/jearles/avSfa/28/

-

ko.bindingHandlers.chosen = {
    init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
        var allBindings = allBindingsAccessor();

        var options = {default: 'Select one...'};
        $.extend(options, allBindings.chosen)

        $(element).attr('data-placeholder', options.default);                
    },
    update: function(element, valueAccessor, allBindingsAccessor, viewModel) {
        $(element).chosen();
    }
};

-

或者,您可以使用setTimeout将调用移至chosen到执行队列的底部.这样,在chosen尝试对其进行转换之前,可以给敲除选项绑定时间来完成其工作.

Alternatively, you can use setTimeout to move the call to chosen to the bottom of the execution queue. This will give the Knockout options binding time to do its work before chosen tries to transform it.

ko.bindingHandlers.chosen = {
    init: function (element, valueAccessor, allBindingAccessor, 
                    viewModel, bindingContext) {
        var options = ko.utils.unwrapObservable(valueAccessor() || {});
        setTimeout(function() { $(element).chosen(options); }, 0);
    }
};

这篇关于如何订购敲除绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆