简体   繁体   English

剔除自定义选择活页夹,具有更新功能

[英]knockout custom select binder with update function

I am trying to develop a custom select binder but I am not able to understand how to develop the update functionality. 我正在尝试开发自定义选择活页夹,但是我不明白如何开发更新功能。 Currently this is what I have done. 目前, 是我所做的。 I want custom binder to handle this type of data 我希望自定义活页夹处理此类数据

[{
    message: "Hello",
    Value: 1
}, {
    message: "Hi",
    Value: 2
}, {
    message: "Bye",
    Value: 3
}, ]

The problem, as I understand it: You are using the Bootstrap Fullscreen Select , and you can initialize it, but when you select a new value, no observable gets updated. 据我了解,问题是:您正在使用Bootstrap Fullscreen Select ,并且可以对其进行初始化,但是当您选择一个新值时,不会观察到任何更新。 Basically, we need to know when a new value is selected. 基本上,我们需要知道何时选择新值。

Since the way the widget works is to pop up a screen of options and allow you to select one, and then close that popup, and the widget provides the ability to do an onClose callback, that's all we need. 由于小部件的工作方式是弹出一个选项屏幕,并允许您选择一个选项,然后关闭该弹出窗口, 并且小部件提供了执行onClose回调的功能,这就是我们所需要的。

I'm using the standard convention of a value binding in the select. 我在select中使用value绑定的标准约定。 When the widget closes, if the new value is different from the bound value, I will update the bound value to the new value selected in the widget. 当小部件关闭时,如果新值与绑定值不同,我将把绑定值更新为在小部件中选择的新值。

ko.bindingHandlers.menu = {
    init: function (element, valueAccessor, allBindingsAccessor, viewModel) {
        var valueObservable = allBindingsAccessor().value;
        $(element).mobileSelect({
            onClose: function () {
                var newValue = $(this).val();
                if (newValue !== valueObservable()) {
                    valueObservable(newValue);
                }
            }
        });
    }
};

As a fiddle: http://jsfiddle.net/4zsu9pv2/4/ 小提琴: http : //jsfiddle.net/4zsu9pv2/4/

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

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