2013-04-24 65 views
7

想知道是否有人在使用Ember時處理點擊格外的更好方法?我知道jQuery的方式與全局點擊處理程序,你必須指定每個動作採取某些實例,但我希望有人想出了一個方法來聲明這在一個Ember視圖。同樣,我嘗試了ol給一個選項卡索引和使用模糊,但Ember行動似乎不允許這樣做。Ember - 處理視圖外的點擊

+1

我不明白是什麼問題。您可以基於regexp表達式來委託事件,這不需要任何瘋狂的事情。即使看一下Ember.js文檔,也表明利用jQuery的'.on()'方法是他們用來解決嵌套或不嵌套元素的大型子集上的委託問題的方法。 – Ohgodwhy 2013-04-24 23:08:27

+1

您可以使用['didInsertElement']事件(http://stackoverflow.com/questions/11377215/can-i-give-the-view-a-show-up-animation-in-emberjs/11377899#11377899)事件啓動一個插件和類似的東西。你應該使用['willDestroyElement'](http://emberjs.com/api/classes/Ember.View.html#event_willDestroyElement)去除綁定。或者,您可以使用['View#on'](http://emberjs.com/api/classes/Ember.View.html#method_on)。 – MilkyWayJoe 2013-04-25 00:15:49

回答

7

感謝您的意見。我又回去閱讀jQuerys .on的文檔。我不知道你可以命名空間事件。所以我把這兩個意見,並結合他們這樣的事情。

didInsertElement: function() { 
    Ember.run.next(this, 'attachClickHandler'); 
}, 

attachClickHandler: function(){ 
    var temp = this; 

    $(window).on("click." + temp.elementId, function (e){ 
     //...event here 
    }); 
}, 

detachClickHandler: function(){ 
    $(window).off("click." + this.elementId); 
}, 

這可以讓事件針對每個視圖實例,這正是我想要的。多謝你們!

+2

我假設從視圖的'willDestroyElement'鉤子中調用'detachClickHandler()'? – Zaki 2013-12-10 09:10:39

+0

我不知道事件namespacing :) – 2014-10-07 06:40:08

1

你會發現ClickElsewhereMixin有用

只是包括任何組件上的混入和實施onClickElsewhere()