6

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.

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?

4

3 回答 3

5

好的,我想通了。Ember.js常见问题解答

另一方面,过滤器对存储缓存中的所有记录执行实时搜索。一旦新记录加载到存储中,过滤器将检查记录是否匹配,如果匹配,则将其添加到搜索结果数组中。如果该数组显示在模板中,它将自动更新。

...

请记住,如果商店不知道这些记录,它们将不会显示在过滤器中。push()您可以使用 store 的方法确保一条记录在 store 中。

因此,在我想要实时更新的视图的控制器中,我filter()在商店中使用来获取模型。

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()有新Comment的存储时,它都会自动添加到视图中的相应帖子中。

于 2013-10-29T10:04:04.060 回答
0

您可能需要将它们显式推送到页面上使用的集合中pushObjectstore.push将返回一个现场记录,所以你可以做这样的事情。

var book_record = store.push('book', model_json);
this.get('controllers.books.content').pushObject(book_record);

这是假设BooksController是一个标准ArrayController

于 2013-10-28T17:11:26.343 回答
0

不幸的是,它需要两个步骤(但步骤很简单)。您需要将其添加到存储中,然后保存记录以通过适配器将更改传播到侦听器。

var pushedRecord = store.push(model, model_json);
pushedRecord.save();

此外,如果您有多个记录,您可以调用pushMany而不是单独推送每个记录。您仍然必须保存每个。

store.pushMany(model, jsonArray).forEach function (pushedInstance) {
  pushedInstance.save();
}
于 2013-10-29T03:07:08.463 回答