如何在回调函数中冒泡 Ember 操作? [英] How can I bubble up an Ember action inside a callback function?
问题描述
我有一个 Ember 应用程序,我正在使用一个操作来应用 CSS 动画.动画完成后,我想将动作从控制器冒泡到我的路由以处理更多功能.
I have an Ember application and I am using an action to apply a CSS animation. Once the animation is complete I want to bubble up the action from the controller to my route to handle further functionality.
我知道如果我 return: true;
操作会冒泡,正如 此处.
I know that if I return: true;
the action will bubble up, as explained here.
这是我的控制器的样子:
This is what my controller looks like:
App.MyController = Ember.ObjectController.extend({
actions: {
myAction: function() {
$('.my-element').addClass('my-animation-class').one('webkitAnimationEnd oanimationend msAnimationEnd animationend', function(e) {
console.log('working');
return true;
}
}
}
});
如果我在我的 animationend
回调中将某些内容记录到我的控制台,我可以看到它在工作,如果我将 return: true;
移到回调之外,动作就会冒泡成功地.但是,在回调内部返回 true 不起作用.
If I log something to my console in my animationend
callback I can see it working and if I move return: true;
outside of the callback, the action bubbles up successfully. However, returning true inside of the callback does not work.
我错过了什么?
推荐答案
你可以通过调用self.target.send('myAction');
一个像这样定义的IndexController
App.IndexController = Ember.Controller.extend({
actions: {
bubbleMe: function(item){
var self = this;
alert('IndexController says you clicked on: ' + item);
setTimeout(function(){
self.target.send('bubbleMe',[item]);
}, 1000);
}
}
});
会冒泡到这样定义的 ApplicationRoute
App.ApplicationRoute = Ember.Route.extend({
actions: {
bubbleMe: function(item){
alert('ApplicationRoute says you clicked on: ' + item);
}
}
});
你可以在这里看到一个工作小提琴:http://jsfiddle.net/tikotzky/2ce9s32f/
You can see a working fiddle here: http://jsfiddle.net/tikotzky/2ce9s32f/
这篇关于如何在回调函数中冒泡 Ember 操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!