I've been doing this with Ember-Data-2.13 that is interacting with a Rails JSONAPI-Resources backend.
Ember Data's rollbackAttributes()
on a model does a great job
of rolling back any attributes that were modified.
The belongs-to
relationships however, these require some extra
love. Here's what I've done; it's pretty simple:
- Override the model's
rollbackAttributes()
to also call a new function namedrollbackBelongsTo()
. - Also on your model, author a function named
preserveBelongsTo()
.
This function manually caches the instances assigned to thebelongs-to
relationship. - Finally on your model, author the
rollbackBelongsTo()
mentioned first.
Here's a simple example of a very basic user model:
// app/models/user.js
export default DS.Model.extend({
/* ---------------------------------------------------------- ROLLBACK HACK */
/**
* Call this function from your component/route immediately
* prior to editing.
*/
preserveBelongsTo() {
this.set('preserved', Ember.Object.create({
telCountryCode: this.get('telCountryCode.content')
}));
},
/**
* Override the basic rollbackAttribute(); in addition call the
* `rollbackBelongsTo()` function.
* @override
*/
rollbackAttributes() {
this._super(...arguments);
this.rollbackBelongsTo();
},
/**
* Rollback the `belongs-to` relationships.
*/
rollbackBelongsTo() {
if (Ember.isPresent(this.get('preserved'))) {
this.set('telCountryCode', this.get('preserved.telCountryCode'));
}
},
/* ------------------------------------------------------------- PROPERTIES */
email: DS.attr('string'),
familyName: DS.attr('string'),
givenName: DS.attr('string'),
telNational: DS.attr('string'),
/* ---------------------------------------------------------- RELATIONSHIPS */
telCountryCode: DS.belongsTo('phone-country-code')
})
It would be great to bind to the Model's rolledBack
event
in an effort to trigger the rolling back of the belongs-to
relationships.
In addition, using the Model's didLoad
and didUpdate
events to
cache the belongs-to relationship values would make life so easy.
Alas, there are too many bugs in Ember-Data for this to work
perfectly. For example, I cannot even get the rolledBack
event
to fire in regular circumstances.
Why not use the Model's relationships
computed property to dynamically cache the belongs-to relationships?
I'm thinking doing just that.