Ember-data: deserialize embedded model

拥有回忆 提交于 2019-12-21 05:35:33

问题


I really don't get 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 instance like so:

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 to store.push('instance', instance);, I receive:

Uncaught Error: Assertion Failed: Ember Data expected a number or string to represent the record(s) in the users relationship instead it found an object. If this is a polymorphic relationship please specify a type key. If this is an embedded relationship please include the DS.EmbeddedRecordsMixin and specify the users property in your serializer's attrs

Where is the mistake?

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

  • http://emberjs.com/api/data/classes/DS.EmbeddedRecordsMixin.html
  • How to make embedded hasMany relationships work with ember data
  • Ember-data Serialize/Deserialize embedded records on 3rd level

Thanks a lot


回答1:


create a serialize inside folder serialized. The name for serialize 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 occurs when you have objects inside an object. If you dont want to create a class like this one for each model you have with objects inside objects, copy this code to application.js inside serializers folder.




回答2:


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

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"
  }]
}

Here it is easy to do:

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


来源:https://stackoverflow.com/questions/27913955/ember-data-deserialize-embedded-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!