How to get access a model data inside controller Ember 2.11.0


#1

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 ?


#2

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


#3

Thanks for your comment.

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


#4

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.


#5

Thanks for explanations