将模型推送到商店时更新视图 [英] Update view when pushing models to the store
问题描述
我的应用程序中有相当复杂的页面,显示了许多不同的模型。我通过一个 / updates
REST调用来更新几个这些模型。我通过一个 last_request_timestamp
参数,它返回自上次调用以来创建或修改的模型。
I have quite a complex page in my application with lots of different models being shown. I live-update several of these models through a /updates
REST call. I pass a last_request_timestamp
parameter and it returns the models that were created or modified since the last call.
我添加使用 store.push(model,model_json)
的商店的模型。但是,在推送模型后,模板不会更新。如何确保商店中的模型与视图之间有约束?
I add the models to the store by using store.push(model, model_json)
. However, the templates are not updated after the models have been pushed. How can I ensure that there is a binding between the models in the store and the view?
推荐答案
好的,我想出了。 Ember.js常见问题解答说
另一方面,过滤器执行商店缓存中所有记录的实时搜索。一旦新的记录被加载到商店,过滤器将检查该记录是否匹配,如果是,则将其添加到搜索结果数组中。如果该数组显示在模板中,它将自动更新。
Filters, on the other hand, perform a live search of all of the records in the store's cache. As soon as a new record is loaded into the store, the filter will check to see if the record matches, and if so, add it to the array of search results. If that array is displayed in a template, it will update automatically.
...
请记住如果商店不知道这些记录,该记录将不会显示在过滤器中。您可以使用商店的 push()
方法确保商店中的记录。
Keep in mind that records will not show up in a filter if the store doesn't know about them. You can ensure that a record is in the store by using the store's push()
method.
所以在控制器中我想要更新的视图,我使用商店中的 filter()
来获取模型。
So in the controller for the view that I want to live-update, I use filter()
on the store to fetch the models.
App.PostController = Ember.ObjectController.extend({
comments: function() {
var postId = this.get('id');
return this.get('store').filter('comment', function(comment) {
return comment.get('post.id') == postId;
});
}.property('comments')
});
现在,每当我 push()
a新的评论
存储,它会自动添加到视图中的相应帖子。
Now, whenever I push()
a new Comment
to store, it is automatically added to the appropriate post in the view.
这篇关于将模型推送到商店时更新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!