![](/img/trans.png)
[英]Is it possible to pass a function to an Ember.js component to execute it as an action callback?
[英]Pass function to an Ember component
是否可以將功能傳遞給組件? 我試圖將代碼從視圖移到組件,以使Ember應用程序向前發展(請參閱此處 ),並且我在整個站點中使用了一堆常用功能。 我不確定如何從組件內部調用它們。 我在舊代碼中將它們放在自己的控制器中,但是我聽說將控制器傳遞給組件是草率的或不明智的。
例如,在組件把手文件中,我正在考慮以下問題:
{{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
遇到的是undefined 。
有什么建議么?
我在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}}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.