简体   繁体   English

Knockout.js:自定义绑定中的数组参数

[英]Knockout.js: array parameter in custom binding

i try to write a custom list-binding. 我尝试编写自定义列表绑定。 This is what i have so far: 这是我到目前为止所拥有的:

var myArr = ko.observableArray();
myArr.push("foo");
myArr.push("bar");

var view = {
    matches: myArr
}

ko.bindingHandlers.matchList = {
        init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
            // gives me 0
            console.log(valueAccessor().length);
            // gives me 2
            console.log(valueAccessor()().length);

        },
    };

// Activates knockout.js
ko.applyBindings(view);

My html binding looks as follow: 我的html绑定如下所示:

<div data-bind="matchList: matches"></div>

Why do i have to use the second pair of parentheses to get into my array? 为什么我必须使用第二对括号才能进入数组?

The valueAccessor is a function that returns what was passed to the binding. valueAccessor是一个函数,它返回传递给绑定的内容。 It is wrapped in a function, so that it is not evaluated immediately. 它包装在一个函数中,因此不会立即对其求值。

A typical pattern would be to do: 一个典型的模式是:

var value = ko.utils.unwrapObservable(valueAccessor());

ko.utils.unwrapObservable will safely handle both observables and non-observables and return the value. ko.utils.unwrapObservable将安全地处理可观察值和不可观察值,并返回值。 So, if it is an observable, then it will return yourValue() , otherwise it will just return yourValue . 因此,如果它是可观察的,则它将返回yourValue() ,否则将仅返回yourValue This allows your binding to support binding against either observables or plain properties. 这使您的绑定支持对可观察属性或纯属性的绑定。

In addition, some bindings need to deal with the observable itself and some bindings need to deal with the value of the observable. 另外,某些绑定需要处理可观察对象本身,而某些绑定需要处理可观察对象的值。 So, the observable is not unwrapped, by default. 因此,默认情况下,可观察对象未展开。 So, valueAccessor() returns your observable (which is a function) and then it is up to you to decide if you want to unwrap it to get the value or possibly set the value of it. 因此,valueAccessor()返回您的observable(它是一个函数),然后由您决定是否要对其进行拆包以获得值或可能设置其值。

I think the confusing thing here is that the valueAccessor passed to init is different from the parameter of the same name passed to update . 我认为这里令人困惑的是,传递给initvalueAccessor与传递给update的同名参数不同。 In init, it's a function that returns functions that in turn returns your array. 在初始化中,它是一个返回函数的函数,而函数又返回您的数组。 Check out this sample code from their documentation . 从他们的文档中查看此示例代码。 I added two console logs at the end that should show you the function that valueAccessor() returns: 我在末尾添加了两个控制台日志,这些日志应该向您显示valueAccessor()返回的函数:

var myArr = ko.observableArray();
myArr.push("foo");
myArr.push("bar");

var view = {
    matches: myArr
}

ko.bindingHandlers.matchList = {
        init: function(element, valueAccessor, allBindingsAccessor, viewModel) {
            var value = ko.utils.unwrapObservable(valueAccessor()); // Get the current value of the current property we're bound to
            $(element).toggle(value); // jQuery will hide/show the element depending on whether "value" or true or false
            console.log(value);
            console.log(valueAccessor().toString());
        }
    };

// Activates knockout.js
ko.applyBindings(view);

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

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