简体   繁体   中英

Passing data from controller to route in Ember

I'm trying to figure out how data received from a controller can be used in a different route in Ember.js.

I've got a controller like this, which is making an Ajax call and upon success, calls the successCallback function:

App.LoginController = Ember.ArrayController.extend({
  ...
  function successCallback(json){
    var userData = json;
    self.transitionToRoute('dashboard');
  }
  ...
});

I'm trying to retrieve this userData in LoginController and use it in the dashboard view.

App.DashboardRoute = Ember.Route.extend({
  model: function(){
    return this.controllerFor('login').get('userData');
  }
});

I've tried consoling out the results of this.controllerFor('login').get('userData'); but nothing returns. But consoling out the data before transitioning in the LoginController shows that the data did get assigned to the variable userData .

Any help would be great!

var userData = json;

declares userData as a local variable, which loses scope once you leave the method. You need to have a userData property on the controller and then set it using

this.set('userData', value);

Then, the value will persist and be available in the dashboard route.

See a working example here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM