简体   繁体   English

将数据从控制器传递到Ember中的路由

[英]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. 我试图弄清楚从控制器接收到的数据如何在Ember.js中的不同路由中使用。

I've got a controller like this, which is making an Ajax call and upon success, calls the successCallback function: 我有一个像这样的控制器,它正在进行Ajax调用,并在调用成功时调用successCallback函数:

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. 我试图在LoginController中检索此userData并在仪表板视图中使用它。

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'); 我试过安慰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 . 但是在过渡到LoginController之前对数据进行整理显示,数据确实已分配给变量userData

Any help would be great! 任何帮助将是巨大的!

var userData = json;

declares userData as a local variable, which loses scope once you leave the method. userData声明为局部变量,一旦离开该方法,它将失去作用域。 You need to have a userData property on the controller and then set it using 您需要在控制器上具有userData属性,然后使用

this.set('userData', value);

Then, the value will persist and be available in the dashboard route. 然后,该值将保留并在仪表板路线中可用。

See a working example here 在这里查看工作示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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