【问题标题】:Emberjs partial's action doesnt fire in componentEmberjs partial 的动作不会在组件中触发
【发布时间】:2014-07-02 12:39:23
【问题描述】:

我尝试扩展一个组件 (https://github.com/ember-addons/ember-forms),以便在表单控件旁边添加额外的按钮。

想法

开发者将一个额外的属性传递给组件,部分将在表单控件(输入、选择、文本区域)旁边呈现。

问题

它工作正常,但如果我有一些动作的部分,动作不会触发。

JsBin

这里有一个简化的 JsBin 来演示这个问题:http://jsbin.com/pexolude/105/edit

html

  <script type="text/x-handlebars">
    <h2>Component test</h2>

    {{outlet}}
  </script>

  <script type="text/x-handlebars" data-template-name="index">
  {{booh-whaa partial="somebutton"}}
  <h3>This partial's action works</h3>
  {{partial "somebutton"}}
  </script>

   <script type="text/x-handlebars" data-template-name='_somebutton'>
  <button {{action "booh"}} >Hit me!</button>
</script>

     <script type="text/x-handlebars" data-template-name='components/booh-whaa'>
  <h3>This is my component</h3>
  {{partial partial}}
</script>

JS.

App = Ember.Application.create();

App.IndexController = Ember.Controller.extend({
  selectedCategory:null,
  actions: {
    booh: function() {
      alert("booh!");
    } 
  }
});

App.BoohWhaaComponent = Ember.Component.extend({

});

【问题讨论】:

    标签: javascript ember.js


    【解决方案1】:

    正如 Knownasilya 所说,默认情况下,组件内的操作由组件处理。但是,如果您在动作助手上指定目标,动作会自动传播,您不必使用sendAction()

    在你的情况下,这很简单:

    {{action 'booh' target='controller'}}
    

    或者如果动作在路线的视野中:

    {{action 'booh' target='parentView'}}
    

    另一种选择是使用 Em.TargetActionSupport 将带有上下文和其他参数的操作发送到整个应用中的特定目标。

    【讨论】:

      【解决方案2】:

      当您在组件内触发操作时,该组件会处理该操作。如果您希望它继续,请使用this.sendAction('action'),然后在您的组件集上使用{{booh-whaa action='booh'}}。有关组件中操作的更多信息,请参阅guides

      这是一个有效的jsbin

      另外,部分也不再需要下划线。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-23
        • 2014-09-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-29
        • 1970-01-01
        相关资源
        最近更新 更多