首页 文章

将函数传递给Ember组件

提问于
浏览
0

是否可以将函数传递给组件?我正试图将我的代码从视图和组件中移开以保持我的Ember应用程序向前移动(请参阅here),并且我认为将控制器传递给组件是草率或不明智的 .

例如,在组件手柄文件中,我正在考虑这个:

{{a-cool-component 
  property1=someValue
  function1=controllers.utils.doSomethingRepetitious 
}}

所以在我的组件javascript代码中我可以做这样的事情......

FacetListItemComponent = Ember['default'].Component.extend({
  property1: null
  property2: null
  function1: null 
  didInsertElement: function() {
    //... do stuff here
    this.set('property2', this.function1(this.get('property1')));
  }
);
exports['default'] = FacetListItemComponent;

我在我正在构建的组件中尝试了它,但 function1 遇到了未定义的问题 .

有什么建议?

我在Ember-CLI 0.2.7上使用Ember 1.11

布赖恩

1 回答

  • 1

    您应该使用get(),而不需要在组件上设置 function1: null .

    //component
    MyExComponent = Ember.Component.extend({
      didInsertElement: function(){
        var fun = this.get('function1');
        fun()
      }
    })
    
    //other component
    
    MyOtherComponent = Ember.Component.extend({
      function1: function(){
        alert('hello');
      }
    })
    
    //other component template
    
    {{my-ex
      function1=function1}}
    

相关问题