How to get access a model data inside controller Ember 2.11.0

Hi guys , I have really big confuse with providing data from route to controller , please help me:

route/application.js

import Ember from 'ember';

export default Ember.Route.extend({

  model() {
    return {
      token: '1213213',
      isLogin: true
    };
  },
  setupController(controller, model) {
    controller.set('model', model);
  }
});

controllers/application.js

import Ember from 'ember';

export default Ember.Controller.extend({

  init() {
    console.log(this.get('model'));
  }

});

And every time I have undefined, but I to open ember tools in chrome we will see this model already exist in controller.

What I am doing wrong ?

1 Like

The model has not yet been set at the time the init method is called.

1 Like

Thanks for your comment.

So I should use some special methods of controller class ? Or need run reopenClass or some another ?

No…what @Gaurav0 is trying to say is… init is technically the class’ constructor. Consider the following piece of over simplified code that shows the execution order.

class Controller {
  constructor() {
    this.init();
  }

  init() {
    console.log(this.get('model')); // This is your get model
  }
}

var instance = new Controller();

instance.set('model', {});

If you follow the execution order, you’ll see that of course model is going to be undefined when you get it.

1 Like

Thanks for explanations