c++ - FlexLex从输入文件读取

我希望能够仅对选择元素应用敲除绑定selectOrDie以一般地应用selectOrDie。
 

ko.bindingHandlers.selectOrDie = {
    init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
        // This will be called when the binding is first applied to an element
        // Set up any initial state, event handlers, etc. here

        $(element).selectOrDie({
            onChange: function() {
                console.log(element);
            }
        });
    },
    update: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
        // This will be called once when the binding is first applied to an element,
        // and again whenever any observables/computeds that are accessed change
        // Update the DOM element based on the supplied values here.
        console.log($(element).siblings('span .sod_label').text());

    }
};


我不知道如何以selectOrDie注册的更改触发绑定更新的方式进行设置。还是有更好的方法?

这是select元素,没什么特别的:

 
            <select data-bind="selectOrDie: $data" data-custom-class="w60">
                <option>10</option>
                <option>20</option>
                <option>30</option>
                <option>50</option>
                <option>Alle</option>
            </select>


如何制作通常将selectOrDie应用于元素的敲除绑定?

最佳答案

最好的方法是使用敲除提供的现有options绑定,以获取视图模型中某个数组或可观察数组与select元素之间的双向数据绑定优势。

然后创建一个单独的绑定处理程序,该处理程序允许将selectOrDie小部件应用于相同的select元素。这是我之前创建的selectOrDie绑定处理程序:

 

ko.bindingHandlers.selectOrDie = {
    init: function (element, valueAccessor, allBindings, viewModel, bindingContext) {
        // apply selectOrDie widget to select element
        $(element).selectOrDie(valueAccessor());

        var subsription, options = allBindings()["options"];

        // check if bounded collection from "options" binding is observable array
        if(options && ko.isObservable(options) && "push" in options){
            // ensure changes to bounded collection update selectOrDie widget
            subscription = options.subscribe(function(){
                $(element).selectOrDie("update");
            });
        }

        // register disposal to clean up after dom node is removed
         ko.utils.domNodeDisposal.addDisposeCallback(element, function() {
            subscription.dispose();
            $(element).selectOrDie("destroy");
        });
    }
};


现在,将此绑定处理程序与现有的options绑定结合使用,您可以执行以下操作:
 
<select data-bind="options: myArray, value: myValue, selectOrDie: { }">
</select>


注意,我已经将一个空对象传递给selectOrDie绑定,您还可以传递一个包含有效configuration options的对象,如下所示:
 
<select data-bind="options: myArray, value: myValue, selectOrDie: {customClass: 'someclass' }">
</select>


检查此工作fiddle
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值