是否可以将函数传递给组件?我正在尝试将我的代码从 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/