当您使用时,this.sendAction('actionName')
您正在冒泡一个动作,您必须使用它来捕捉组件/控制器actions
//controller/route/component.js
actions: {
actionName: function() {
//Do something
}
}
如果你想把它发送到链上,你必须sendAction('')
再次调用组件/控制器并在父级上再次捕获它(等等)。
另一种方法this.get('action')()
使用闭包动作,这是常规的 javascript 函数。据我所知,这些是在 Ember 1.13.X 中调用操作的首选方式。闭包动作的一件巧妙的事情是你可以有返回值。这意味着你可以拥有这样的东西:
//a controller
actions: {
saveResult() {
return this.get('model').save(); //notice the return (which returns a promise)
}
}
//some template that uses the controller above
{{a-component save=(action 'saveResult')}} // Passes the saveResult action to the component
//a-component.js
actions: {
someAction: function() {
this.attrs.save().then(() => {
//Do something with the return value
});
}
}
关于闭包动作可以写很多,但其他人写得比我好得多,所以我推荐以下文章:
如果您对整个 DDAU(Data Down Actions Up)概念不熟悉,我真的推荐Sam关于该概念的文章。
更新:还有一个插件(由@locks 在评论中链接)允许关闭操作冒泡到 routes。请注意,如果您希望将 Ember 升级到更新的版本(3.8 及更高版本),route-action
s 将不兼容。