重新评估计算的淘汰赛这只是取决于可观察到的阵列上 [英] Reevaluating a Knockout computed which depends just on an observable array

查看:87
本文介绍了重新评估计算的淘汰赛这只是取决于可观察到的阵列上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我Appmodel包括注释可观察到的数组

My Appmodel consists of an observable array of comments

self.comments=ko.observableArray([]);  // Default Value is an empty array
/*
   Here comes some code to initially fill the observable array 
   with items from an JSON Response
*/

此外,我有两个computeds应该重新present的第一注释,最后一个注释

Furthermore I have two computeds which should represent the very first comment and the last comment

self.firstComment = ko.computed(function () {
    var sorted = self.comments.sort(function (left, right) {
        return left.Id() - right.Id();
    });
    return sorted[0];
});

self.lastComment = ko.computed(function () {
    var sorted = self.comments.sort(function (left, right) {
        return left.Id() - right.Id();
    });
    return sorted[sorted.length - 1];
});

这完美的作品在初始化应用程序(从服务器加载JSON,建立应用程序模型......),但是当我添加了评论阵,computeds不承认的数组项的数量发生了变化(我的理解是,数组属性本身观察可观察到的阵列仅仅是可观察到的)。所以,当我做的:

This works perfectly on initializing the application (loading the JSON from Server, build up App model...), but when I add a comment to the array, the computeds do not recognize that the number of array items has changed (as I understood it, an observable array is just an observable where the array properties themselves are observed). So when I do:

self.comments.push(aNewCommentObject);

self.lastComment仍然绑定到阵列的项目,这是当应用程序最初加载。

self.lastComment is still bound to the array item, that it was when the app loaded initially.

我已经找到<一个href=\"http://blog.greatrexpectations.com/2012/07/12/forcing-computed-observables-to-refresh-in-knockout/\"相对=nofollow>这个博客帖子如何通过引入一个虚拟的观察到的给力计算,但我不喜欢这种方法。出于什么目的则是使用的observableArray又如何?

I have found this blog post how to force computation by introducing a dummy observable, but I don't like the approach. For what purpose is an observableArray used then and how?

额外的挑战:我想保持在每一种情况排序observableArray项目(因为它这应该只是按时间顺序排列的注释饲料)。我试图做到这一点的蒙山计算 commentsSorted ,但也有问题,当observableArray有新的项目,所以同样的问题在这里这不更新。这就是原因,为什么我每次排序在firstComment和lastComment。

Additional Challenge: I would like to keep the observableArray Items sorted under every circumstance (because its a comment feed which should be just sorted chronologically). I tried to do this whith an computed commentsSorted but also have problems that this does not update when the observableArray has new items, so same problem here. Thats the reason, why I am sorting everytime in firstComment and lastComment.

推荐答案

尝试展开的评论引发淘汰赛的依赖追踪:

Try unwrapping the comments to trigger Knockout's dependency tracking:

self.firstComment = ko.computed(function () {
    var sorted = self.comments().sort(function (left, right) {
        // -------------------^^ !
        return left.Id() - right.Id();
    });
    return sorted[0];
});

或(同一件事):<​​/ p>

or (same thing):

self.firstComment = ko.computed(function () {
    var sorted = ko.unwrap(self.comments).sort(function (left, right) {
        return left.Id() - right.Id();
    });
    return sorted[0];
});

当然,你可以抽象成这样一个独立的计算。

Of course you can abstract this into a separate computed.

self.commentsSorted = ko.computed(function () {
    return self.comments().sort(function (left, right) {
        return left.Id() - right.Id();
    });
});

self.firstComment = ko.computed(function () {
    return ko.unwrap(self.commentsSorted)[0];
});

由于computeds缓存其返回值(就像所有其他观察到​​的一样),你不必担心调用 self.commentsSorted 多次。它会重新计算的基本可观察到的阵列机会时才会出现。

Since computeds cache their return values (just like every other observable does) you don't need to worry about calling self.commentsSorted multiple times. It will re-calculate only when the underlying observable array chances.

这篇关于重新评估计算的淘汰赛这只是取决于可观察到的阵列上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆