lundi 8 août 2016

Related model data cannot be loaded alongside this list of data of same model

I have two models "book" and "tag" which have many-many relationship.

// book model
import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr(),
  age: DS.attr(),
  admin: DS.attr(),
  avatar: DS.attr(),
  author: DS.belongsTo('author'),
  tags: DS.hasMany('tag'),
  isValid: Ember.computed.notEmpty('name')
});


// tag model
import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr(),
  books: DS.hasMany('book', {async: true})
});

Now, I am trying to create an edit form where I require all the tags to be listed as checkbox and then pre-select the associated ones.

So in my route, I have setup my edit route like this:

export default Ember.Route.extend({
  model(params) {
    return this.get('store').findRecord('book', params.book_id, {
      include: 'author, tags',
      async: false
    });
  },
  afterModel() {
    var self = this;
    return Ember.RSVP.hash({
      author: this.get('store').findAll('author'),
      tags: this.get('store').findAll('tag')
    }).then(function(result) {
      self.set('authors', result.authors);
      self.set('tags', result.tags);
    });
  },
  setupController(controller, model) {
    this._super(controller, model);
    controller.set('authors', this.get('authors'));
    controller.set('tags', this.get('tags'));
  }
});

Now, in my view just for testing I have printed out the lengths of both tags as:

collection:  
association: 

The problem here is that "model.tags.length" is initally show but as soon as the collection "tags" is loaded, it is reset to 0 and only "tags.length" is present.

I played around with the code and now I think the "ember-data" "store" is sharing 'tags' for both cases as when I dont load the collection, associated tags are shown just fine.

So, what is the problem here ? Am I missing out on something or is this some random bug ?

Please help. Been stuck here for too long.




Aucun commentaire:

Enregistrer un commentaire