Wilhearts
Wilhearts

Reputation: 169

Ember data FixtureAdapter hasmany - Cannot call method 'toString' of undefined

again struggling with ember-data >.< , the bug here is on the FixtureAdapter, whenever i use a hasmany relationship, i get the errors Error while loading route: TypeError {} Cannot call method 'toString' of undefined"

Library versions

Ember: 1.1.3+pre.e0ffbf84
Ember Data: 1.0.0-beta.3
Handlebars: 1.0.0 
jQuery: 1.9.1 

The code, also here in a JSBin

Uncomment the items: hasMany line in the Facture model to reproduce the bug..

Thanks for the help, it might be a bug, in which case i will post an issue on github.

// Setup
App = Ember.Application.create({});
App.ApplicationAdapter = DS.FixtureAdapter;

// Models
// Facture

App.Facture = DS.Model.extend({
    title: DS.attr(),
    createdAt: DS.attr('date', { defaultValue: new Date() })
    // Comment this line out and it does display a title
    //,items: DS.hasMany('item', { embedded: true })
});

// - Items
App.Item = DS.Model.extend({
    desc: DS.attr(),
    qty: DS.attr("number", { defaultValue: 0 }),
    price: DS.attr("string", { defaultValue: 0 })
});

// FIXTURES

App.Facture.FIXTURES = [
    {
        id: 1,
        title: "Services Informatiques",
        createdAt: new Date(),
        items: [
          { id: 1, desc:'Keay', qty: 2, price: "45" },
          { id: 2, desc:'You kidding', qty: 5, price: "75" }
        ]
    },
    {
        id: 2,
        title: "Intégration Web",
        createdAt: new Date(),
        items: [
          { id: 1, desc:'lkelzekekl', qty: 2, price: "250" },
          { id: 2, desc:'You', qty: 5, price: "200" }
        ]
    }
];

// Routes
App.IndexRoute = Ember.Route.extend({
  model: function(){
      return this.store.find('facture');
  }
});

Upvotes: 0

Views: 1178

Answers (1)

Marcio Junior
Marcio Junior

Reputation: 19128

It seems that the RESTAdapter and FixtureAdapter doesn't have support for embedded associations by default. Just ActiveModelAdapter.

There is some workarounds, with the FixtureAdapter you can use your fixtures, without the embedded data:

App.Facture.FIXTURES = [
    {
        id: 1,
        title: "Services Informatiques",
        createdAt: new Date(),
        items: [1,2]
    },
    {
        id: 2,
        title: "Intégration Web",
        createdAt: new Date(),
        items: [3,4]
    }
];

App.Item.FIXTURES = [
  { id: 1, desc:'Keay', qty: 2, price: "45" },
  { id: 2, desc:'You kidding', qty: 5, price: "75" },
  { id: 3, desc:'lkelzekekl', qty: 2, price: "250" },
  { id: 4, desc:'You', qty: 5, price: "200" }
]

This is a jsbin with this sample http://jsbin.com/oTIkigAV/9/edit

To use with ActiveModelAdapter, just provide the configuration:

App.ApplicationAdapter = DS.ActiveModelAdapter;

// serializer for Facture model
App.FactureSerializer = DS.ActiveModelSerializer.extend({
  attrs: {
    items: {embedded: 'always'}
  }
});

Using { embedded: true } in hasMany('item') don't worked for me :(.

And your payload need to have the type: embeddedModel property. In your case type: 'item'. Like the following:

{
    factures: [
        {
            id: 1,
            title: "Services Informatiques",
            createdAt: newDate(),
            items: [
                {
                    id: 1,
                    desc: 'Keay',
                    qty: 2,
                    price: "45",
                    type: 'item'
                },
                {
                    id: 2,
                    desc: 'Youkidding',
                    qty: 5,
                    price: "75",
                    type: 'item'
                }
            ]
        },
        {
            id: 2,
            title: "Intégration Web",
            createdAt: newDate(),
            items: [
                {
                    id: 3,
                    desc: 'lkelzekekl',
                    qty: 2,
                    price: "250",
                    type: 'item'
                },
                {
                    id: 4,
                    desc: 'You',
                    qty: 5,
                    price: "200",
                    type: 'item'
                }
            ]
        }
    ]
};

And a jsbin with active model adapter http://jsbin.com/oTIkigAV/10/edit

Upvotes: 2

Related Questions