我有一个 Controller A
发送了一个操作 this.send('makeItHappen')
,我想在 Controller B
中处理它。我该怎么做?
JS:
// controllers/documents/datasets/controller-A
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
sendToDataCenter() {
this.send('makeItHappen'); // this throws an error
}
}
});
// controllers/controller-B
import Ember from 'ember';
export default Ember.Controller.extend({
actions: {
makeItHappen() {
console.log('It works!!');
}
}
});
在 Controller B 中,它抛出一个错误:
Uncaught Error: Nothing handled the action 'makeItHappen'. If you did handle the action, this error can be caused by returning true from an action handler in a controller, causing the action to bubble.
请问有人可以帮忙吗?谢谢。
最佳答案
一般来说,如果没有定义,每条路由都会有一个默认 Controller 。
在 Controller A 中,这行代码 this.send('makeItHappen');
将在数据表、文档、应用程序 Controller 的操作哈希中查找 makeItHappen
方法如果 makeItHappen
在任何地方定义,则其相应的路由不会出现此错误。
为了实现您的需要,
目前,在您的路由/ Controller 层次结构中, Controller A 和 Controller B 之间没有父子关系。所以你可以将 Controller B 注入(inject) Controller A 中并直接调用 makeItHappen
。
// controllers/documents/datasets/controller-A
import Ember from 'ember';
export default Ember.Controller.extend({
controllerB:Ember.inject.controller('controller-B');//this should be already instantiated ie,this corresponding route should be visited earlier otherwise you will get `unknown injection: controller:users' Error
actions: {
sendToDataCenter() {
this.get('controllerB').send('makeItHappen');
}
}
});
关于javascript - 在 Ember 中,如何在另一个 Controller 中处理从一个 Controller 发送的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41679208/