Question

still trying to build an invoicing app using ember-model and hasMany relationships using the FixtureAdapter for now. 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 ? :/ 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

Thanks for any help, could not find an answer here.

A Simplified version reproducing the problem on 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>

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();
      }
    }
});
Was it helpful?

Solution

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top