lundi 19 juin 2017

Saving to multiple nested models in Ember

I have a form with fields consisting of nested models, but I can't get the data to persist--perplexingly, the data appears initially in all the routes, and it shows up when I navigate between them, but it disappears after reloading the page.

The data in the label model (the parent) persists, but none of the data for the nested models does. I have set up serializers and I suspect that the problem is in my saveEntry action.

How can I get the data to save and persist? Why does it work initially, but not repeatedly?

Here are some models:

// app/models/label.js  

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  artists: DS.hasMany('artist')
});

// app/models/artist.js

import DS from 'ember-data';

export default DS.Model.extend({
  name: DS.attr('string'),
  label: DS.belongsTo('label'),
  albums: DS.hasMany('albums')
});

Here's the serializer:

// app/serializers/label.js

import ApplicationSerializer from './application';

export default ApplicationSerializer.extend(DS.EmbeddedRecordsMixin, {
    attrs: {
        artists: {
          embedded: 'always'
        }
     }
});

Here's a basic form:

// app/templates/index.hbs

<form>
    
    
    
    
    <button type="button" >Add</button>
</form>

Here's the action:

// app/routes.index.js

createEntry() {
      let route = this;
      let controller = this.get('controller');
      let label = this.store.createRecord('label', {
        name: controller.get('newLabel'),
        artist: route.store.createRecord('artist', {
          name: controller.get('newArtist'),
          album: this.store.createRecord('album', {
            name: controller.get('newAlbum'),
            song: this.store.createRecord('song', {
              name: controller.get('newSong')
            })
          })
        })
      });




Aucun commentaire:

Enregistrer un commentaire