简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM