javascript - 将函数传递给 Ember 组件

标签 javascript ember.js

是否可以将函数传递给组件?我正在尝试将我的代码从 View 移至组件,以保持我的 Ember 应用程序向前发展(请参阅 here ),并且我在整个网站中使用了一堆常用功能。我不确定如何从组件内调用它们。我已将它们放在旧代码中自己的 Controller 中,但我听说将 Controller 传递给组件是草率或不明智的。

例如,在组件 Handlebars 文件中,我正在考虑:

{{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

布莱恩

最佳答案

您应该使用 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}}

关于javascript - 将函数传递给 Ember 组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31192612/

相关文章:

javascript - 如何以编程方式关闭 Node 程序并重新启动它而不使用任何库?

javascript - 成功调用函数后的 Angular $timeout

javascript - 缩小 httpInterceptor AngularJS $injector 错误

javascript - 如何在 Javascript 中使用 PHP 中的 JSON 数据(数组)

javascript - iOS 对话框中的 HTML 滚动问题

ember.js - 使用 Ember.js Data RESTAdapter 时应如何处理错误?

javascript - EmberJs - 将值与模板绑定(bind)时出现问题

javascript - $(window).click(function(e) - 当前点击元素的标识符

javascript - Ember.js map 操作 keyUp 事件

javascript - 访问嵌套组件中的父属性