2014-07-16 26 views
4

sendAction()在Ember.Component氣泡控制器默認情況下是預期的。但我有2,3行動,我寧願發送到使用該組件的相應視圖。在模板中,我們設置了使用target=view查看的操作。我們能做到嗎?Ember.Component的sendAction()查看目標

更新:目前作爲解決方案,我將我的視圖對象發送到從那裏調用view.send()發送操作的組件。但我覺得這是不正確的。

+1

我相信,如果您只是在組件的模板中使用{{action「someAction」}}並在組件的動作散列中處理它,而組件的目標組件本身就是這樣做的。 – elrick

+0

對不起,如果我的英語很差..但我所要求的是,當我們接觸到組件的動作哈希,如果我需要冒泡行動,我們需要使用'sendAction()'方法。但是,那麼只有泡到控制器。我無法冒泡查看使用該組件的內容。 – thecodejack

+0

我想我知道你的意思現在會提交一個答案來闡述。 – elrick

回答

6

好了之後,有些想法我相信我知道你的意思。如果你有一個組件並且你有一個動作,它將由組件自己處理。如果你想在組件外發送一個動作,你可以使用sendAction。

我們的目標,其中握着你的分量,因爲你的組件是底座上的觀點來看,你可能可以做this.get('parentView')獲取父視圖,然後連鎖send('nameOfAction')

因此,這將是this.get('parentView').send('nameOfAction')從組件內操作,然後觸發組件所嵌入的父視圖上的操作。

所以在你的組件,你可以有:

App.DemoCompComponent = Ember.Component.extend({ 
    actions: { 
    internalTrigger: function() { 
     //just an alert to make sure it fired 
     alert('Internal action was caught on component'); 
     //this should target parent view component is in 
     this.get('parentView').send('viewTriggerTest'); 
    } 
    } 
}); 

現在讓我們說你有你的,你可以在指數模板組件:

模板將是:

<script type="text/x-handlebars" data-template-name="index"> 
    <h2>Inside Index Template</h2> 
     {{demo-comp}} 
    </script> 

的索引查看代碼將是:

App.IndexView = Ember.View.extend({ 
    actions: { 
    viewTriggerTest: function() { 
     alert('View Trigger Test Worked on Index!'); 
    } 
    } 
}); 

這裏是一個jsbin http://emberjs.jsbin.com/reyexuko/1/edit

+0

謝謝..我不期待this.get('parentView')在組件:) :) – thecodejack

+0

沒問題的人。我也學到了一些東西。 – elrick

+1

如果您看下[Ember API文檔中的類]的標題(http://emberjs.com/api/classes/Ember.Component.html),則會指定哪個類已被擴展以創建類有問題。正如你所看到的,Em.Component擴展了Em.View,並且parentView屬性可用。 –

0

有關最新的灰燼2.9推薦的方法是將closure action傳遞給子組件。該物業targetparentView是私人的。