简体   繁体   中英

Creating New Record using Ember-Model

I struggled for hours trying to get this to work using Ember-Data with no result, so thought I give Ember-Model a try; still no joy.

I have the following:

App.Item = Ember.Model.extend({
   itemName: Ember.attr(),
});

App.Strat = Ember.Model.extend({
    stratName: Ember.attr(),
    items: Ember.hasMany('App.Item',{key: 'items', embedded: true});

App.Strat.adapter = Ember.FixtureAdapter.create();

App.Strat.FIXTURES =
[
  {id: 1, stratName: 's1', items: 
     [{id: 1, itemName: 'I1'}]},
  {id: 2, stratName: 's2', items:
     [{id: 2, itemName: 'I2'},
      {id: 3, itemName: 'l3'}]}
];

Everything seemed to work fine up to this point. I'm able to display the fixture data via the templates. What I want to do is to allow the user to add additional strat records, by showing a pre-populated strat record on the screen, allowing users to make modifications, then save it with the two strat records loaded from the fixture data. I tried the following:

var dummyStrat =
  {id: 100, stratName: "s5", items: 
      [{id: 101, itemName: "I5", strategy: 100}]};

var newStrat = App.Strat.create (dummyStrat);
newStrat.save();

This generated the following error:

TypeError: this.get(key).toJSON is not a function.

But no error if I did this:

var dummyStrat = 
  {id: 100, stratName: "s5"};

var newStrat = App.Strat.create (dummyStrat);
newStrat.save();

What am I doing wrong?

What happens if you remove strategy: 100 from the create call? I have pretty much the same code in my app except I'm not setting any variable that isn't already an attribute in my model.

My other suggestion would be to try renaming the items key to stratItems so that the key doesn't have the same name as the computed hasMany attribute.

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