Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update view when pushing models to the store

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?

like image 692
yorbro Avatar asked Oct 28 '13 16:10

yorbro


1 Answers

Ok, I figured it out. The Ember.js FAQ says

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.

...

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.

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')
});

Now, whenever I push() a new Comment to store, it is automatically added to the appropriate post in the view.

like image 132
yorbro Avatar answered Nov 01 '22 09:11

yorbro