Ember-data: inline model deserialization

I don't really understand it here.

I have the following code:

App.Instance = DS.Model.extend({
  hash: DS.attr('string'),
  users: DS.hasMany('user', { embedded: 'always' })
});

App.User = DS.Model.extend({
  name: DS.attr('string'),
  color: DS.attr('string'),
  lat: DS.attr('number'),
  lng: DS.attr('number'),
  instance: DS.belongsTo('instance')
});

App.InstanceSerializer = DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
  attrs: {
    users: { embedded: 'always' }
  }
});

      

And an example is this:

var instance = {
  hash: "68309966ec7fbaac",
  id: "54b4518fcbe12d5160771ebe",
  users: [{
    color: "#9E463C",
    id: "78b662bc56169a96",
    lat: 36.5299487,
    lng: -6.2921774,
    name: "User 1"
  },{
    color: "#9E463C",
    id: "78b662bc56169a96",
    lat: 36.5299487,
    lng: -6.2921774,
    name: "User 2"
  }]
}

      

But when I want store.push('instance', instance);

, I get:

Unaccepted Error: Assertion Failed: Ember Data is expecting a number or string to represent the record (s) in a relationship users

, it encountered an object instead. If it's a polymorphic relationship, provide a key type

. If this is a built-in link, please include DS.EmbeddedRecordsMixin

and specify the property users

in your serialization parameterizer

Where is the mistake?

Read from all of these sources, which always use a different strategy:

Thank you so much

+3


source to share


2 answers


As from this article: http://mozmonkey.com/2013/12/loading-json-with-embedded-records-into-ember-data-1-0-0-beta/ ember wants to disable your data somehow:

var data = {
  instance: {
    hash: "68309966ec7fbaac",
    id: "54b4518fcbe12d5160771ebe",
    users: ["78b662bc56169a96", "78b662bc56169a97"]
  },
  users: [{
    color: "#9E463C",
    id: "78b662bc56169a96",
    lat: 36.5299487,
    lng: -6.2921774,
    name: "User 1"
  },{
    color: "#9E463C",
    id: "78b662bc56169a96",
    lat: 36.5299487,
    lng: -6.2921774,
    name: "User 2"
  }]
}

      



It's easy to do here:

for (var i=0; i < data.users.length; i++) {
  store.push('user', data.users[i]);
}
store.push('instance', data.instance);

      

-1


source


create serializable serialization inside folder. The name for serialization is the same as your model.

import DS from 'ember-data';

export default DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, {
    hasEmbeddedAlwaysOption: function(attr) {
        var option = this.attrsOption(attr);

        if (typeof(option) === 'undefined') {
            return true;
        } else if (option.embedded === false) {
            return false;
        }

        return this._super.apply(this, arguments);
    },

    noSerializeOptionSpecified: function(attr) {
        var option = this.attrsOption(attr);

        if (typeof(option) === 'undefined') {
            return false;
        } else if (option.embedded === false && !!option.serialize) {
            return true;
        }

        return this._super.apply(this, arguments);
    }
});

      



This happens when you have objects inside an object. If you don't want to create a class like this for every model that has objects within objects, copy this code into application.js inside the serializers folder.

0


source







All Articles