0

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?

spirito_libero
  • 1,206
  • 2
  • 13
  • 21

2 Answers2

0

I fear you are looking at non-official and outdated API docs. The API docs for Ember Data are hosted here: https://api.emberjs.com/ember-data/release

The rollbackAttribute() method is not documented anymore for the latest release, which is 3.13 at the time writing this. It was last documented for 3.1. I think it was removed as a stale feature flag in this PR: [FEAT] remove all stale feature flags #5384

jelhan
  • 6,149
  • 1
  • 19
  • 35
0

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);
  },
});
spirito_libero
  • 1,206
  • 2
  • 13
  • 21