Assigning a hasMany Relationship in Ember Data

I am trying to figure out Ember Data models and try to assign a collection of found objects using store.findAll() for the hasMany relation of the ember data model.

I have two models:

 App.Leaf = DS.Model.extend({ text: DS.attr('string'), branch: DS.belongsTo('App.Branch') }); App.Branch = DS.Model.extend({ lotsOfLeaves: DS.hasMany('App.Leaf') }); 

If I assign a link as part of createRecord()

  App.store.loadMany(App.Leaf,[ { id: 1, text: "Hello, I'm leaf 1", branch_id: 1 }, { id: 2, text: "Hello, I'm leaf 2", branch_id: 1 } ]); var allLeaves = App.store.findAll(App.Leaf); var oneBranch = App.Branch.createRecord({ id: 1, lotsOfLeaves: allLeaves }); 

then it fails (silently) since oneBranch.get('lotsOfLeaves.length') is 0 .

Similarly, it fails if I assign a connection after association:

  var all = App.store.findAll(App.Leaf); oneBranch.set('lotsOfLeaves', all); 

I understand that I can use pushObject() on oneBranch.get('lotsOfLeaves') to add each sheet individually, but is this the only way?

+6
source share
1 answer

The MutableArray class also defines the pushObjects () method, which you must use to add all of them at once.

The documentation is here: http://docs.emberjs.com/symbols/Ember.MutableArray.html#method=pushObjects

+4
source

Source: https://habr.com/ru/post/926121/


All Articles