3

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:

Thanks a lot

Community
  • 1
  • 1
Augustin Riedinger
  • 20,909
  • 29
  • 133
  • 206
  • Your instance object has errors in it. Look at the id string and missing comma. Also, specifying an id on your instance and users may be messing up the store. Try changing those id's to a new attribute that represents what that id actually is. – rogMaHall Jan 13 '15 at 16:02
  • Thanks for replying. The instance errors were typos copying from console. I tried removing the id from the user, but it doesn't help. If I remove id from instance, I get `Uncaught Error: Assertion Failed: You must include an id for instance in an object passed to push`. – Augustin Riedinger Jan 14 '15 at 13:39

2 Answers2

0

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.

Pedro Romão
  • 2,285
  • 28
  • 22
-1

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);
Augustin Riedinger
  • 20,909
  • 29
  • 133
  • 206
  • 4
    Sideloading is not embedding. We know how to sideload. The OP's question is why is embedding failing, even thought the embedded option was properly specified within the serializer. –  Apr 29 '15 at 06:47