Manu Benjamin
Manu Benjamin

Reputation: 997

Ember - How to reload route model after save/update records

I want to reload my route model after save/update action in the route.js file. Following is my route model.

export default Ember.Route.extend({

model() {
  return Ember.RSVP.hash({
        employeeList : this.store.findAll('employee'),
        employee : this.store.createRecord("employee"),
      });
}

I tried following and it doesn't worked.

this.refresh(); // I saw this in many posts.

Can anyone suggest how to reload a model after save/update operation?

Upvotes: 2

Views: 6073

Answers (4)

Paul Oliver
Paul Oliver

Reputation: 7671

I'm wondering if it's a timing thing. New records show up in the data store for me great when I use a subroute...i.e. routes/employees.js returns this.store.findAll('employee') while routes/employees/new.js returns this.store.createRecord('employee').

If a new route isn't an option, perhaps a promise will help?

export default Ember.Route.extend({

  model() {
    // forces a createRecord after the store is already loaded with all employees
    var newEmployeePromise = new Ember.RSVP.Promise((resolve, reject) => {
      let allEmployees = this.store.findAll('employee'); 
      allEmployees.then(() => {
        resolve(this.store.createRecord('employee')); 
      }, (error) => {
        reject(error); 
      });
    });

    return Ember.RSVP.hash({
      employeeList : this.store.findAll('employee'),
      employee : newEmployeePromise,
    });
  }
}

Upvotes: 1

Ember Freak
Ember Freak

Reputation: 12872

Creating new record through this.store.createRecord() ,will update the store automatically, so you don't need to refresh the page, since it will update ui automatically.
If you still you need to update the page, like you said you can call this.refresh().. You please have a look at twiddle

If you provide ember-twiddle for non working.it would be good.

Upvotes: 1

kristjan reinhold
kristjan reinhold

Reputation: 2048

Try to use store.push method.

"push is used to notify Ember Data's store of new or updated records that exist in the backend. This will return a record in the loaded.saved state"

store.push(store.normalize('employee', savedEmployee));

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

Upvotes: 2

Gupta
Gupta

Reputation: 10368

you could use setupController hook to fix the issue.

import Ember from 'ember';

export default Ember.Route.extend({

  model() {
    return Ember.RSVP.hash({
        employeeList : this.store.findAll('employee'),
        employee : this.store.createRecord("employee"),
    })

  setupController(controller, model) {
    controller.set('employeeList', model.employee);
  }
});

Hope this help you !!!

Upvotes: 1

Related Questions