Accessing data from the controller in the didInsertElement view

I am trying to access data from a DashboardIndexController in a DashboardIndexView

JP.DashboardIndexController =  Ember.Controller.extend({
  users: []
});

Is it possible to access users in JP.DashboardIndexView in the didInsertElement file?

didInsertElement : function(){
    console.log(this.get("controller.users").objectAt(0));
}

This is my DashboardIndexRoute:

JP.DashboardIndexRoute = Ember.Route.extend({
  setupController: function(controller, model) {
    controller.set('users',  JP.User.find());
  }
});

thank

EDIT

console.log(this.get("controller.users").objectAt(0));

It only returns data when I switch to UsersIndex, and then back to DashboardIndex... I think this is something with initialization, but I don’t know how to fix it.

+5
source share
1 answer

Yes, here's how to access the list of users.

This is because it is DashboardIndexViewinserted into the DOM before it is controller.userspopulated with data from the server.

, , controller.users - ajax, , didInsertElement .

, controller.users . didInsertElement - , JP.User.find().

, , JP.User.find() , DOM. - :

JP.DashboardIndexView = Ember.View.extend({
  didInsertElement: function() {
    this.usersChanged();
  },
  usersChanged: function() {
    if (!this.$()) { return; } // View not in DOM
    console.log(this.get('controller.users'));
  }.observes('controller.users.@each')
});
+9

All Articles