samedi 9 avril 2016

Ember Data rollback ralationships

I made a JsBin to provide a test environment for this. It has two models Account and Company. Account belongsTo Company.

Accounts route list all the accounts. Clicking on one of the account brings to the the detailed account view where all the companies are listed. Clicking on one of the company's name changes the account's company.

The rollback button should rollback the company to the original one. For this I use a mixin

  relationships: {},
  cacheRelationships: function(){
    var _this = this, value;
    console.log('cacheRelationships');
    this.eachRelationship((name, relationship) => {
      if (relationship.kind === 'belongsTo') {
        console.log('belongsTo relationship: ' + name);
        value = _this.belongsTo(name).value();
        console.log('value: ' + value);
        if (value) {
          _this.relationships[name] = value;
        }
      }

      if (relationship.kind === 'hasMany') {
        console.log('hasMany relationship: ' + name);
        value = _this.hasMany(name).value();
        console.log('value: ' + value);
        if (value) {
          _this.relationships[name] = _this.hasMany(name).ids();
        }
      }
    });
  }.on('ready'),
  didUpdate: function() {
    console.log('didUpdate');
  },
  rollback: function() {
    console.log('rollback');
    this.rollbackAttributes();
    var _this = this;
    this.eachRelationship((name, relationship) => {
      if (relationship.kind === 'belongsTo') {
        console.log(_this.relationships[name]);
      }
      if (relationship.kind === 'hasMany') {
        console.log(_this.relationships[name]);
      }
    });
  }

It should cache all the relationships of the model on ready event, using Ember Model's new ds-references API. The problem: nothing is cached.




Aucun commentaire:

Enregistrer un commentaire