繁体   English   中英

Ember模型的create()未创建ID和适配器的适配器必须实现“ createRecord”

[英]Ember-model create() not creating an ID and adapter must implement “createRecord”

暂时仍在尝试使用ember-model和hasMany关系来构建发票应用程序。 当我尝试创建新的发票记录时,我得到的新发票没有错误,但是根本没有ID(未定义),我应该处理ID创建服务器端吗? :/并且当我尝试使用任何发票创建新项目(发票中有很多项目)时,在调用save时出现错误Èmber.Adapter must implement createRecord

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

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

的index.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

暂无
暂无

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

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