简体   繁体   English

使用Ember-Model创建新记录

[英]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; 我费了好几个小时才能尝试使用Ember-Data使其无果而终,所以我想尝试一下Ember-Model。 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. TypeError:this.get(key).toJSON不是一个函数。

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? 如果从创建调用中删除策略:100,会发生什么? 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. 我的另一建议是尝试将items键重命名为stratItems,以使键的名称与计算出的hasMany属性的名称不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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