Ember模型create()没有创建ID,适配器必须实现“;createRecord”;

Ember-model create() not creating an ID and adapter must implement "createRecord"

本文关键字:适配器 实现 createRecord ID create 模型 创建 Ember      更新时间:2023-09-26

目前仍在尝试使用ember模型构建发票应用程序,并使用FixtureAdapter构建hasMany关系。当我试图创建一个新的发票记录时,我得到了一个没有错误的新发票,但根本没有ID(它是未定义的),我应该在服务器端处理ID创建吗?:/当我尝试在任何发票中创建新项目时(发票有许多项目),当调用save时,我得到错误Èmber.Adapter must implement createRecord

谢谢你的帮助,在这里找不到答案。

在JSBIN上重现问题的简化版本

索引.html

<script type="text/x-handlebars" data-template-name="factures">
{{#each}}
    <ul>
        <li>{{#link-to 'facture' this}}
        {{#if id}}
        #{{id}}:{{title}}
        {{else}}
        #undefined:{{title}}
        {{/if}}
        {{/link-to}}</li>
    </ul>
{{/each}}
<button {{ action 'newInvoice' }}>New Invoice using create() then save()</button>
  <hr/>
    {{outlet}}
</script>
<script type="text/x-handlebars" data-template-name="facture">
<h2>Items for {{title}}</h2>
{{#each items}}
<ul>
  <li>{{desc}}</li>
</ul>
{{/each}}
<button {{ action 'newItem' }}>New Item using create() then save()</button>
</script>

控制器处理动作

App.FactureController = Ember.ObjectController.extend({
  actions: {
    newItem: function(){
      var items = this.get('model').get('items');
      items.create({desc:"New Item"});
      items.save();
    }
  }
});
App.FacturesController = Ember.ArrayController.extend({
    actions: {
      newInvoice: function(){
        var facture = App.Facture.create({title:"New Invoice"}),
        comment = facture.get('items').create({desc:"Sample Item"});
        facture.save();
      }
    }
});

是的,通常客户端不知道下一个id应该是什么,所以当您创建并保存模型时,服务器应该返回一个id来更新模型。如果你愿意,你可以随机生成一个,但这可能不是一个真实的场景(除非模型只存在于客户端)

你面临的问题是,当你尝试保存和项目时,它不知道如何保存。您需要定义一个用于Item模型的适配器,以便它知道在哪里/如何保存该模型类型。

http://emberjs.jsbin.com/eKibapI/7/edit