Search code examples
javascriptember.jsjavascript-framework

Ember.js cannot set content of ArrayController


Suppose I have an ArrayController:

CellarRails.SearchController = Ember.ArrayController.extend({
  content: []
});

and a SearchRoute:

CellarRails.SearchRoute = Ember.Route.extend({
  model: function(params) {
    console.log('MODEL HOOKED!!');
    return CellarRails.Track.find(params);
  }
});

and a find method in model:

CellarRails.Track.reopenClass({
  find: function(params) {
    ...
    some code
    ...
    return result;
  }
});

PROBLEM: The result array is returning properly, model hook is fired, but content of controller is undefined and it's length is 0, so what am I doing wrong?


Solution

  • You should add the setupController hook and setting the content to the model returned by your find() operation:

    CellarRails.SearchRoute = Ember.Route.extend({
      model: function(params) {
        console.log('MODEL HOOKED!!');
        return CellarRails.Track.find(params);
      },
      setupController: function(controller, model) {
        controller.set('content', model);
      }
    });
    

    Update, in response to your last comment

    See here for a working demo.

    Hope it helps.