2
votes

Is there a way to reload the whole store in ember-data? Or to sort items inside?

It should go like that:

  • user clicks "add" button
  • new record is added
  • store is commited
  • user is transmited to the state where items are listed
  • his new entry is there in correct order

To do this I have to either transmit in model.didCreate which shouldn't be (it's not the role of a model!) or refresh store after transmiting.

Anyone had similar issues?

I am using ember-data revision 12

EDIT

I used:

Ember.run.later(this, function(){
    this.transitionToRoute('list');         
}, 500);

To give store some time. But then again new entry is always at the bottom of the list. I still need to reload whole store or sort it somehow.

EDIT#2

So I did little rework here. In my template I use:

{{#each arrangedContent}}

To use sorted data and in the route I have:

App.AdsRoute = Ember.Route.extend({
    model: function(){
        return App.Ad.find();
    },
    sortProperties: ['id']
});

It's not working though. Looks like Route is not ArrayController. And if I make additional AdsController extending from ArrayController it is not working neither.

1

1 Answers

1
votes

Ember ArrayControllers have a few options for sorting the content:

http://emberjs.com/api/classes/Ember.ArrayController.html#property_sortAscending

You can set which fields to sort by and which order you want to use. When you use these you will want to bind views to arrangedContent and not just plain old content.

In my applications I typically return to the list state after the record has been persisted:

var observer,   _this = this;

observer = function(target, path) {   
  if (target.get(path) === 'saved') {
    target.removeObserver(path, null, observer);
    return _this.get('target.router').transitionTo('apps.index');   
  } 
};

I'm not sure if the store has a reload mechanism (individual records do), but I can update this response if I find an answer.