【问题标题】:How can I bubble up an Ember action inside a callback function?如何在回调函数中冒泡 Ember 操作?
【发布时间】:2014-10-01 02:16:17
【问题描述】:

我有一个 Ember 应用程序,我正在使用一个动作来应用 CSS 动画。动画完成后,我想将动作从控制器冒泡到我的路由以处理更多功能。

我知道如果我 return: true; 操作会冒泡,正如 here 所解释的那样。

这是我的控制器的样子:

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 是行不通的。

我错过了什么?

【问题讨论】:

    标签: ember.js callback controller return event-propagation


    【解决方案1】:

    您可以通过调用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/

    【讨论】:

      猜你喜欢
      • 2016-01-03
      • 1970-01-01
      • 2016-03-24
      • 1970-01-01
      • 1970-01-01
      • 2013-11-15
      • 2019-06-20
      • 1970-01-01
      • 2014-08-26
      相关资源
      最近更新 更多