简体   繁体   English

敲除“选项”绑定的自定义绑定

[英]Knockout Custom Binding for “options” Binding

I am using knockout options binding like this: 我正在使用像这样的剔除选项绑定:

 <select id="mydata" data-bind="options: mydata,
             optionsText:  function(item) {return getText(item); },
                 optionsValue:'dataId',
                 optionsCaption:'Choose...'">

</select>

I want to apply jquery Chosen plugin to the dropdown and whenever `"mydata" changes, I'd like to apply chosen update. 我想将jquery Chosen插件应用于下拉列表,每当“ mydata”更改时,我都想应用所选的更新。 So I figure the best way to do it is through ko custom binding. 因此,我认为最好的方法是通过ko自定义绑定。 I found an simple example online, essentially a wrapper for "options" binding,like this: 我在网上找到了一个简单的示例,实质上是“选项”绑定的包装,如下所示:

ko.bindingHandlers.chosen = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        ko.bindingHandlers.options.init(element);
        $(element).chosen(); //i added this
    },
    update: function (element, valueAccessor, allBindingsAccessor, viewModel) {
         ko.bindingHandlers.options.update(element, valueAccessor, allBindingsAccessor);
        $(element).trigger("chosen:updated");//i added this
    }

}; };

But where do I specify the optionsText, optionsValue, OptionvCaptions in this custom binding? 但是optionsText, optionsValue, OptionvCaptions在此自定义绑定中optionsText, optionsValue, OptionvCaptions我应在哪里指定optionsText, optionsValue, OptionvCaptions Thanks. 谢谢。

optionsText, optionsValue, OptionvCaptions are all part of the allBindingsAccessor parameter. optionsText, optionsValue, OptionvCaptions都是allBindingsAccessor参数的一部分。 So, you can either specify them in your data-bind, like you normally would, and pass them along, which is what your posted code does, or you could type in the defaults in your update binding. 因此,您可以像通常那样在数据绑定中指定它们,然后传递它们,这就是您发布的代码所做的,或者您可以在更新绑定中键入默认值。 I think what you really want to do is specify them in your templates and not put application specific logic in your bindings, but if you really want to... 我认为您真正想要做的是在模板中指定它们,而不是在绑定中放入特定于应用程序的逻辑,但是如果您确实想要...

ko.bindingHandlers.chosen = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        //options.init only takes one parameter
        ko.bindingHandlers.options.init(element);
        $(element).chosen(); //i added this
    },
    update: function (element, valueAccessor, allBindingsAccessor) {
         //options.update takes 3 parameters
         //we want the chosen binding to have defaults, if they aren't specified alr
         allBindingsAccessor.optionsText = allBindingsAccessor.optionsText || function(item) {return window.MyApplication.GetText(item)};
         allBindingsAccessor.optionsValue = allBindingsAccessor.optionsValue || 'dataId';
         ko.bindingHandlers.options.update(element, valueAccessor, allBindingsAccessor);
        $(element).trigger("chosen:updated");//i added this
    }

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

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