Ember has method rollbackAttribute()
which is very similar to the default method rollbackAttributes()
. The difference is rollbackAttribute()
can be used to rollback ONLY specific model attribute.
By default this method is not available and to use it you need to enable ds-rollback-attribute
and run the canary build as written here: https://docs.w3cub.com/ember/classes/ds.model/methods/#rollbackAttribute
Where can I enable the ds-rollback-attribute
and how can I ran the canary build
?
Actually the implementation of the rollbackAttribute()
is quite simple.
We can create our own method and extract it into the service
.
app/services/rollback-attribute.js
import Ember from 'ember';
export default Ember.Service.extend({
rollback(model, attribute) {
const changedAttributes = model.changedAttributes();
if (changedAttributes[attribute]) {
model.set(attribute, changedAttributes[attribute][0]);
}
}
});
After creating this service you can use it for example in the route.js
import Ember from 'ember';
import service from 'ember-service/inject';
export default Ember.Route.extend({
rollbackAttribute: service('rollback-attribute'),
_rollbackAttribute(model, attribute) {
this.get('rollbackAttribute').rollback(model, key);
},
});