Parijat Kalia
Parijat Kalia

Reputation: 5085

Loading dynamic data for a route in EmberJS : (Error: Object has no method addArrayObserver)

I am struggling with this piece of code really bad @ the moment. I have a dynamic segment that I specify in my route as :

App.Router.map(function(){
    this.resource('set', {path:'/set/:set_id'}); 
});

And accordingly,

 App.SetRoute = Ember.Route.extend({
      model: function(params){
            return this.get('store').find('gift',params.set_id); // each set has a bunch of gifts that belong to it and are identified by set_id which acts as the primary key .
            }
    });

 App.SetController = Ember.ArrayController.extend(); // array controller since I expect an array of gifts to be returned when visiting this route

When this is executed I get a glaring error that says:

Object[Object Object] has no method addArrayObserver

My data is being loaded through fixture which is something like this :

App.Set.FIXTURES = [
        {
                id: 1,
                name: 'EmberJS'
        },
        {
                id:2,
                name: 'Routing'
        },
     ... ]

App.Gift.FIXTURES = [ { id : 1, gift_name: 'random name', set_id: 1 },

    {
            id: 2,
            gift_name: 'name 2',
            set_id: 1
    },
   ..]

The Gift model is extended as :

App.Card = DS.Model.extend({
        gift_name: DS.attr('string'),   
        set_id : DS.belongsTo('App.Set')
});

The route is loaded by a #linkTo helper tag within handlebars and upon clicking a set linkTo , It renders as expected like :

/set/1 or /set/2

but it does not load any data. I want it to load specific gift data that belongs to the set but it keeps giving me an:

Object [object Object] has no method 'addArrayObserver' error .

Where am I going wrong or what am I missing here ?

Upvotes: 0

Views: 799

Answers (2)

Rainer Schreiber
Rainer Schreiber

Reputation: 11

Set is a singleton, with a one-to-many relation to gifts.

Your SetController must be of type ObjectController, your data something like:

App.Set.FIXTURES = {
id: 1,
name: 'some name',
gifts: [{
id: 1,
name: 'some name'
},{
id: 2,
name: 'some name'
}]
};

Upvotes: 1

Marcio Junior
Marcio Junior

Reputation: 19128

This happen when you have a controller that subclass the Ember.ArrayController in your case App.SetController, but the data set in the content property from the controller is an object instead of an array.

I think that this.get('store').find('gift',params.set_id); is causing this problem. Because find return the first object that mathes the value of params.set_id.

Ember.ArrayController expect an array in your content property, and Ember.ObjectController an object. In your case I recommend to use the Ember.ObjectController, because you are handling one object.

I hope it helps

Upvotes: 1

Related Questions