Adi GuN
Adi GuN

Reputation: 1294

How to update model data when store is updated emberjs

I know Controllers are deprecated now in Ember, but I would like know how I would achieve this using older version of ember.

I have a route defined as follows:

Ember.Route.extend({
    model : function(params) {
         return Ember.RSVP.hash({
             posts : store.find('post', {date : params.date, page : params.page})
         });
    },
    setupController : function(controller, model) {
        this.set('model', model.posts);
    }
});

Everything works fine and the data gets loaded. Now I've decided to load data dynamically from controller. So in the corresponding controller, I did as below:

Ember.ArrayController.extend({
    Ember.RSVP.hash({
             posts : store.find('post', {date : params.date, page : params.page})
    }).then(function(data) {
        this.store.pushPayLoad(this.store.modelFor('post'), data); 
    }.bind(this));
});

In the above way, I was able to get the data loaded in Ember Store but it is not loaded into the model. What is the recommended way so that the model gets updated as soon as the data is available in the store.

Upvotes: 1

Views: 1234

Answers (1)

joshfarrant
joshfarrant

Reputation: 1099

Just to clarify: why do you want to move things to the controller?

It seems that the simplest way to go about all of this would be to get rid off all of that ArrayController code, as well as the setupController method in the route, and just set the model as follows:

Ember.Route.extend({
    model : function(params) {
         return this.store.find('post', {date : params.date, page : params.page});

    }
});

This is Ember's preferred (and expected) way of fetching a model with params.

To simplify things even more, and assuming your params are formatted as follows:

{
  date: SOME_DATE,
  page: SOME_PAGE
}

Then all you need to do is the following:

Ember.Route.extend({
    model : function(params) {
         return this.store.find('post', params);
    }
});

That will find all of the posts with the given params, and assign them to the model, which will then be available as {{model}} in your template and this.get('model'); in your controller.

Upvotes: 1

Related Questions