mercredi 4 novembre 2015

ember data not saving foreign key, sent as null

My ember app is not sending my foreign key to the back-end.

I have a table called issues which is has a related table called categories

My model is:

import DS from 'ember-data';

export default DS.Model.extend({
    name:           DS.attr('string'),
    category_id:    DS.belongsTo('category'),
    description:    DS.attr('string')
});

My route is:

import Ember from 'ember';

export default Ember.Route.extend({
  model: function(){
    return this.store.findAll('issue');
  },
  actions: {
    create: function(){

        var issue = this.store.createRecord('issue');
        issue.name = this.get('controller').get('newName');
        issue.description = this.get('controller').get('newDescription');
        issue.category_id = parseInt(this.get('controller').get('newCategory'));
        //debugger;

        console.log(issue);
        issue.save();
    },
    ...
    other actions 
    ...
    }
  }
});

the console.log from above looks like the category_id is getting set correctly:

category_id: 3
description: "foobar"
name: "test"

However my JSON payload that gets sent to the backend looks like:

{"issue":{"name":"test","description":"foobar","category_id":null}}

I tried stepping through by adding a custom serialiser in app/serializers/application.js

export default DS.RESTSerializer.extend({
  ...

    serialize: function(snapshot,options){
      console.debug('options='+options);
      debugger;
      var json = this._super(snapshot, options);;

      return json;
    }
  ...
  });

But I got lost in all the super calling super indirection.

The snapshot.record has category_id: 3, but the json coming back from the this._super() call has category_id: null

options has includeID:true

Any clues will be much appreciated ...

Ember : 2.0.2
Ember Data : 2.0.0




Aucun commentaire:

Enregistrer un commentaire