【问题标题】:Ember.JS - sending a message from inner controller to outer controllerEmber.JS - 从内部控制器向外部控制器发送消息
【发布时间】:2014-06-13 14:43:54
【问题描述】:

在 Ember 中,我有一个应用程序,其中有一个外部控制器,其模板在标准 {{outlet}} 中呈现。

但是,模板包含一个命名的插座 {{outlet details}},并且该控制器的其中一个操作执行 transitionToRoute(),在该“详细信息”插座内呈现,并且有一个内部控制器处理该嵌套模板.

我需要做的是在内部模板/控制器上执行一个动作,在外部控制器上触发一个动作。 Ember 似乎无法打破“出口”的障碍。事件只会在 outlet 内冒泡,而不是到父 outlet。

我尝试过的事情:

  1. 试过 this.send() 和 this.sendAction()。
  2. 尝试将外部控制器注入内部控制器(需要:“main”,main:Ember.computed.alias("controllers.main")) - 导致“未定义不是函数”错误。
  3. 在嵌套模板中使用 {{action}} 标记并发送父控制器上存在的操作 - 导致“没有处理操作 'selectTab'。如果您确实处理了该操作,则可能会导致此错误通过从控制器中的操作处理程序返回 true,导致操作冒泡”错误。

我正在尝试的可能吗?我知道我可以使用 jQuery 来模拟这个,但我宁愿不必这样做,我更喜欢用“ember 方式”(如果有的话)。

【问题讨论】:

    标签: ember.js


    【解决方案1】:

    第二个作品

    App.IndexRoute = Ember.Route.extend({
      model: function() {
        return ['red', 'yellow', 'blue'];
      },
      renderTemplate: function(){
        this.render();
        var fooCont = this.controllerFor('foo');
        this.render('foo',{
          into:'application',
          outlet:'foo',
          controller:fooCont
        });
      }
    });
    
    App.ApplicationController = Em.Controller.extend({
      actions:{
        hello: function(val){
          alert(val);
        }
      }
    });
    
    App.FooController = Em.Controller.extend({
      needs:'application',
      application: Em.computed.alias('controllers.application'),
      actions:{
        sendToApp: function(){
          this.get('application').send('hello', 'world');
        }
      }
    });
    

    http://emberjs.jsbin.com/qatepibu/1/edit

    【讨论】:

    • Kingpin2k... 你是多产的史诗! :)
    猜你喜欢
    • 2011-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-03
    • 1970-01-01
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多