With ember-data-1.0.0-beta.10 I am using the following model extension.
Just call model.reloadRelationship(name) , where name is the name of the model attribute representing the relation.
This works for both normal and link belongs to / hasMany relationships.
DS.Model.reopen({ reloadRelationship: function(name) { var meta = this.constructor.metaForProperty(name), link = this._data.links ? this._data.links[meta.key] : null; if (!link) { if (meta.kind === 'belongsTo') { this.get(name).then(function(model) { model.reload(); }); } else { this.get(name).invoke('reload'); } } else { meta.type = this.constructor.typeForRelationship(name); if (meta.kind === 'belongsTo') { this.store.findBelongsTo(this, link, meta); } else { this.store.findHasMany(this, link, meta); } } } });
The only thing missing here is some checks, for example checking if the model is already reloading, when the model is reloading a link or checking if the property name exists in the current model.
EDIT ember-data-1.0.0-beta.14:
DS.Model.reopen({ reloadRelationship: function(key) { var record = this._relationships[key]; if (record.relationshipMeta.kind === 'belongsTo') { return this.reloadBelongsTo(key); } else { return this.reloadHasMany(key); } }, reloadHasMany: function(key) { var record = this._relationships[key]; return record.reload(); }, reloadBelongsTo: function(key) { var record = this._relationships[key]; if (record.link) { return record.fetchLink(); } else { record = this.store.getById(record.relationshipMeta.type, this._data[key]); return record.get('isEmpty') ? this.get(key) : record.reload(); } } });
The HasMany relationship will be returned to the built-in reload method.
For a BelongsTo relationship, first check if you need to reload the record (if it is not already loaded up to this point, it will only call to receive the record, otherwise it will cause a reboot).
jcbvm Sep 18 '14 at 21:35 2014-09-18 21:35
source share