简体   繁体   English

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

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

still trying to build an invoicing app using ember-model and hasMany relationships using the FixtureAdapter for now. 暂时仍在尝试使用ember-model和hasMany关系来构建发票应用程序。 When i try to create a new invoice record, i get a new invoice without error, but without an ID at all (it is undefined), am i supposed to handle the id creation server-side ? 当我尝试创建新的发票记录时,我得到的新发票没有错误,但是根本没有ID(未定义),我应该处理ID创建服务器端吗? :/ and when i try in any invoice to create a new items (an invoice hasMany items), when calling save i get error Èmber.Adapter must implement createRecord :/并且当我尝试使用任何发票创建新项目(发票中有很多项目)时,在调用save时出现错误Èmber.Adapter must implement createRecord

Thanks for any help, could not find an answer here. 感谢您的帮助,在这里找不到答案。

A Simplified version reproducing the problem on JSBIN 在JSBIN上重现该问题的简化版本

Index.html 的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>

Controller handling action 控制器处理动作

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();
      }
    }
});

Yeah, generally client side has no idea of what the next id should be, so when you create a model and save it, the server should return an id to update the model with. 是的,通常客户端不知道下一个ID应该是什么,因此在创建模型并保存它时,服务器应返回一个ID来更新模型。 If you want you can randomly generate one, but that's probably not a real life scenario (unless a model only ever lives client side) 如果您愿意,可以随机生成一个,但那可能不是现实生活中的情况(除非模型仅存在于客户端)

The issue you're facing is when you try and save and item it doesn't know how. 您面临的问题是当您尝试保存和记录它时不知道如何操作。 You need to define an adapter to use for the Item model so it knows where/how to save that model type. 您需要定义一个用于Item模型的适配器,以便它知道在何处/如何保存该模型类型。

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

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

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