Asherlc
Asherlc

Reputation: 1161

Refresh plural route model when new record is saved

I have a route that looks like so:

model() {
  return this.store.query('my-model', {
    offset: this.get('offset'),
    limit: this.get('limit')
  })
}

I use this in a route like /my-models, and render a list of the models.

Then, in a route like /my-models/new, I make and save a new model. However, when I save the new model, the list of models doesn't get updated to reflect the new presence of the new model.

I think this is because store.query doesn't monitor the store, it only fetches from the server. However, when I try store.findAll, which I believe should monitor the store for changes, it returns every record, not just the ones between the offset/limit params.

Is there any way to keep the pagination working, while adding having newly created records automatically add to the list?

Upvotes: 0

Views: 354

Answers (2)

Gennady Dogaev
Gennady Dogaev

Reputation: 5991

You can use push method after you saved new model. Smth like this:

var store = this.get('store');
var newModel = store.createRecord('my-model');
newModel.set('param', 'param value')
  .save()
  .then(function () { store.push('my-model', newModel); })

Docs: http://emberjs.com/api/data/classes/DS.Store.html#method_push

Upvotes: 1

Frank Treacy
Frank Treacy

Reputation: 3716

I'm not sure about this one, but if you are using Ember Data 1.13 you could try with the new reload settings.

The documentation for findAll (which I know is not what you want) says:

store.findAll('user');  //goes to the server the first time
store.findAll('user');  //after that returns from cache, but updates in background
store.findAll('user', { reload: true });  //enforces getting fresh data
store.findAll('user', { backgroundReload: false });  //opts out of background updating

If you try backgroundReload: true with query (as third argument) would that refresh the query in the background?

Upvotes: 1

Related Questions