English 中文(简体)
Knockout nested ObservableArrays似乎是没有定义的,而不是空洞的。 [闭门]
原标题:Knockout nested observableArrays appear to be undefined instead of empty. [closed]

如果我对空阵列的空阵列([])采用加固式可观测阵列的功能,我就会发现内部阵列没有界定的错误。 只有在空阵作为参数建造的可观测的Array物体上才会发生这种情况。

这可能是一种ug,但也许/完全是,我只是失踪了。

以下例子与空阵线合著:

http://jsfiddle.net/adamtolley/4pZhR/32/“rel=”_http://jsfiddle.net/adamtolley/4pZhR/32/。

传真:

    <ul data-bind="template: { name:  outerTmpl , foreach: outerArray}"></ul>
Number of inner items: <span data-bind="text: innerCount"></span>
<hr />

<div data-bind="text: ko.toJSON(viewModel)"></div>

<script id="outerTmpl" type="text/html">
    <li>
        <span data-bind="text: name"></span>
        <ul data-bind="template: { name:  innerTmpl , foreach: innerArray}"></ul>
    </li>
</script>

<script id="innerTmpl" type="text/html">
    <li>
        <span data-bind="text: name" />
    </li>
</script>

The JS:

function outer(name, innerArray) {
    return {
        name: ko.observable(name),
        innerArray: ko.observableArray(innerArray)
    };
}

function inner(name) {
    return {
        name: ko.observable(name)
    };
}

var viewModel = {
    outerArray: ko.observableArray([
        new outer("outer1", [new inner("inner1"), new inner("inner2")]),
        new outer("outer2", [new inner("inner1"), new inner("inner2")]) //,
        // new outer("outer3", []) //does not work with this line uncommented. 
    ])
};

//use of innerArray().length vs innerArray.length seems to make no difference in error   
viewModel.innerCount = ko.dependentObservable(function() {
    return this.outerArray().reduce(function(i, j) {
        return i.innerArray.length + j.innerArray.length;
    });
}, viewModel);


ko.applyBindings(viewModel);
最佳回答

我认为,你们想要的是:

viewModel.innerCount = ko.dependentObservable(function() {
    return this.outerArray().reduce(function(i, j) {
        var count = i.innerArray ? i.innerArray().length : i;
        return count + j.innerArray().length;
    });
}, viewModel);

问题在于,减少将通过在下个代号上的以往结果。 因此,你只有两阵列在首批中。

问题回答

暂无回答




相关问题
How to go from DOM node to viewModel object?

When the drop function is called back, this is set to the droppable DOM node (the target) and ui.draggable is the DOM node which was dragged. Is there an idiomatic way of getting the model object ...

making fields observable after ajax retrieval in knockout.js

I am wondering how I can make certain fields observables in knockout.js that I get from an ajax call without having to define the whole object in my viewmodel. Is this possible? Here is what I have ...

ASP.NET MVC Validation with jQuery $.ajax

I have a situation where I am sending data to a Controller via jQuery $.ajax, the data is a serialized json string. (MVC 3.0) It binds it fine - my controller receives the results and they are ...

Knockout.js and MVC

Just started playing with knockout.Js which is a fantastic framework Steve s really done well with that one. One thing I can t seem to do at the minute is impliment it with my Html Helpers. So for ...

Anyone using Knockoutjs with asp.net-mvc in anger? [closed]

I find it very interesting and have a prototype working based on Steve s mvc sample and another small sample from this thread. Using json.net to deserialize within the post action as I couldn t ...

热门标签