简体   繁体   English

Knockout.js 计算出的 observable 没有绑定到 observable 数组

[英]Knockout.js Computed observable not binding to observable array

I'm trying to get computed value and push these values to the observable array.我正在尝试获取计算值并将这些值推送到可观察数组。 Web Api controller returning simple values - just pure numbers without json wrap. Web Api 控制器返回简单值 - 只是没有 json 包装的纯数字。 May be this is issue.可能是这个问题。 Please advise as I'm newbie to Knockout.请提供建议,因为我是 Knockout 的新手。 PS Here is two ajax call because they are calling different api's controllers. PS 这是两个 ajax 调用,因为它们调用了不同的 api 控制器。

 function GrafikViewModel(grafikUri) {
            var self = this;
            self.books = ko.observableArray();
            self.PaxLeft = ko.computed(function () {
                ko.utils.arrayForEach(self.books(), function (book) {
                    $.getJSON("/api/orders/getpax/" + book.kodg, function (item) {
                        self.books().push(item);
                    });
                }, GrafikViewModel)
            });
            $.getJSON(grafikUri, function (data) {
                self.books(data.$values);
            });
        }

I have to do a little guess work, but I believe what you want is我必须做一些猜测工作,但我相信你想要的是

self.books.push(item);

instead of代替

self.books().push(item);

Note the missing () there.注意那里缺少的() The knockout observable array "mirrors" most of the array's functions.淘汰赛可观察数组“反映”了该数组的大部分功能。 By using those instead of the ones provided by Array , knockout can keep track of changes happening to the wrapped array.通过使用这些而不是Array提供的那些,knockout 可以跟踪包装数组发生的变化。

EDIT: You can find a list of all the "mirrored" functions at http://knockoutjs.com/documentation/observableArrays.html#manipulating-an-observablearray , about two pages down (section "Manipulating an observableArray")编辑:您可以在http://knockoutjs.com/documentation/observableArrays.html#manipulating-an-observablearray找到所有“镜像”函数的列表,大约向下两页(“操作 observableArray”部分)

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

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