xamenrax
xamenrax

Reputation: 1744

Ember.js right way of records creation

Cheers! I have models structure like this:

App.Foo = DS.Model.extend({
  bars: DS.hasMany('App.Bar')
});

App.Bar = DS.Model.extend({
  number: DS.attr('number'),
  foo: DS.belongsTo('App.Foo')
});

Is it ok to create App.Bar record first? And if yes, then how to create App.Foo in future and associate already existing Bars records to it in right way? I just want to know, if there something like 'ember-way' in such situations?

Upvotes: 0

Views: 75

Answers (1)

Mike Grassotti
Mike Grassotti

Reputation: 19050

Is it ok to create App.Bar record first?

Yes.

if yes, then how to create App.Foo in future and associate already existing Bars records to it in right way?

bar = store.createRecord(App.Bar);
store.commit();

//later
foo = store.createRecord(App.Foo);
store.commit();

//later
foo.get('bars').addObject(comment);
store.commit();

For some more detailed examples see one-to-many-relationship-tests

Upvotes: 1

Related Questions