0
votes

I have a ember application whose route, controller are as follows:

//Route
model:function(){
    return this.store.getJSON('object/');
},
setupController:function(controller, model){
    controller.set('content', model)
}


//controller
init:function(){
    this.myfunction();
},
myfunction:function(){
  var content = this.get('content')
  console.log(content.length)
}

myfunction gets executed when the page/template is loaded and it returns "cannot read property of null" . I am unable to get route model in controller init function.

1
This has to do with the way controllers are setup relative to routes. By the time setupController has been called in your route, your controller has already been init-ed - acorncom

1 Answers

0
votes

The problem is that this is not referring to the the controller because of the way you called myfunction. In your controller, myfunction should be an action:

init() {
  this.send('myfunction');
},
actions: {
  myfunction: function() {
    var content = this.get('content');
    console.log(content.length);
  }
}