【发布时间】:2016-06-14 22:36:31
【问题描述】:
每次将控制器转换到(“建模器”路线)时,我都需要触发控制器操作,但我似乎无法让它工作。我的 router.js 文件是:
var Router = Ember.Router.extend({
location: config.locationType,
actions: {
loading: function() {
this.controllerFor('modeller').send('loading');
},
didTransition: function() {
this.controllerFor('modeller').send('didTransition');
}
}
});
Router.map(function() {
this.route('modeller', { path: 'modeller'});
this.route('welcome', { path: '/'});
});
export default Router;
还有我的建模控制器:
export default Ember.Controller.extend({
needs: 'modeller',
loading: function () {
console.log("loading")
},
didTransition: function () {
console.log("didTransition")
},
});
但我无法将这些操作中的任何一个记录到控制台。任何指针都非常感谢。
更新:
可能应该提到我使用的是 Ember 2.1.0!
所以我使用的解决方案是 setupController - 但我还需要将它放入 app/routes/modeller.js 下的特定建模器路由文件中。以前我试图在 app/router.js 中做所有事情,但它在那里不起作用。所以最终的解决方案是:
# app/routes/modeller.js
import Ember from 'ember';
export default Ember.Route.extend({
setupController: function(controller, model) {
this.controllerFor('modeller').activate();
}
});
和..
# app/controllers/modeller.js
export default Ember.Controller.extend({
needs: 'modeller',
activate: function () {
console.log("activate")
},
});
【问题讨论】:
标签: javascript ember.js