Is there an event when update parts of a model from backend?


#1

We have the following model:

// app/models/user.js
import Ember from 'ember';
import DS from 'ember-data';

export default DS.model.extend({
  login    : DS.attr('string'),
  language : DS.attr('string'),
  homepage : DS.attr('string'),
  firstname: DS.attr('string'),
  lastname : DS.attr('string')
})

We have a login route where a user is loaded (with a this.get('store').queryRecord('user', ...)) from the backend. Only login, language and homepage attributes are loaded. So we have a user instance with these attributes defined in the store and other are undefined.

Another route display a user list where this previous user is displayed too. We use this.get('store').query('user', queryparams) to load this list. Only firstname and lastname of each user are loaded so the previous user instance (loaded in login route) is updated in the store.

We need to to know when this update occurs : is there an event about this? Thanks for your help,


#2

DS.Model has a ‘didUpdate’ event so you could subscribe to that (pretty sure you can do it on just the one record you’re concerned with, like theRecord.on('didUpdate', function()....)). Alternatively you could put a .then(...) on the query in your model hook because when that query resolves is when the record will be updated. Depending on your needs that may not be what you want though, I’m not sure offhand if the record is actually updated before or after the .then function would be executed. .


#3

Thank you for your proposal. But the ‘didUpdate’ event is fired after saving/sending the record to backend. In my case, only the record in the store is updated.

Yes i could use a .then but my example here is simple : I would like something more generic because i have a lot of models that works like this :worried: