使用Knockout.js加载数据时延迟依赖关系 [英] deferring dependencies when loading data using knockoutjs

查看:199
本文介绍了使用Knockout.js加载数据时延迟依赖关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的用户登录时,我从服务器加载了一堆数据.这会花费很多时间,因为似乎对observableArray()的每次添加都会更新依赖的Observable.我想推迟所有依赖项的评估,直到所有数据加载完毕.我已经看到了如何使用deferEvaluation: true来影响特定ko.computed()变量的行为,但是我想隔离可观察对象,直到我知道它已完成更新,然后使整个模型无效以重绘视图.

When my user logs in, I load a bunch of data from the server. This take a lot of time because it seems that the dependent observables are updated on every addition to an observableArray(). I would like to defer the evaluation of all dependencies until all data are loaded. I have seen how deferEvaluation: true can be used to affect the behavior of a specific ko.computed() variable, but I would like to isolate the observable until I know it's done being updated, and then invalidate the entire model to redraw the view.

我创建了一个 jsfiddle 来说明问题.请注意,在我的真实代码中,我正在使用映射插件加载对象,但是在这里,我只是使用.subscribe函数模拟所有依赖关系,该函数在每次状态更改时都会被调用.我想阻止它直到最后被调用.

I've created a jsfiddle to illustrate the problem. Note that in my real code, I am using the mapping plugin to load objects, but here I just simulate all the dependencies with a .subscribe function that gets called on every state change. I would like to prevent it from being called until the very end.

推荐答案

通常,在这种情况下,您将从observableArray访问基础数组并将其推入.然后,您可以将最后一个推送到observableArray或调用myObservableArray.valueHasMutated().

Typically, in a situation like this you would access the underlying array from an observableArray and push to it. Then, you would either push the last one to the observableArray or call myObservableArray.valueHasMutated().

因此,通常看起来像这样:

So, in general it would look like:

var underlyingArray = this.items();
underlyingArray.push(one);
underlyingArray.push(two);
underlyingArray.push(three);
this.items.valueHasMutated();

在您的小提琴中,您可以执行this.document().history().push(this);.然后,如果您希望运行订阅,则必须重新浏览并在受影响的文档上调用valueHasMutated().

In your fiddle, you could do this.document().history().push(this);. Then, you would have to go back through and call valueHasMutated() on the the affected documents, if you wanted your subscription to run.

这篇关于使用Knockout.js加载数据时延迟依赖关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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