2014-11-08 18 views
1

我使用IronRouter將我的應用程序構建到控制器(路由器)中,我開始附加我的事件。到現在爲止還挺好。收聽模板以外的事件,但僅在模板存在時

然後我開始使用{{#contentFor ...}}功能,此後,我的事件不再觸發,因爲我渲染相關元素的區域是不同DOM樹的一部分。

是否有任何方法仍然聽這些事件?我認爲只是增加一個全局監聽器,但這會消除事件的很大靈活性。

的CoffeeScript:

MyRouter = RouteController.extend(...) 
MyRouter.events(
    'click .inside': (event) -> 
    alert("inside") # works 
    'click .outside': (event) -> 
    alert("outside") # does not work 
) 

HTML:

<button type="button" class="inside"> 
    inside 
</button> 

{{#contentFor 'anotherDomRegion'}} 
    <button type="button" class="outside"> 
    outside 
    </button> 
{{/contentFor}} 

和佈局文件:

<h1>event demo</h1> 
<div> 
    {{> yield}} 
</div> 
<div> 
    {{> yield 'anotherDomRegion'}} 
</div> 

回答

1

我碰到相同問題我自己。我能夠解決它的唯一方法是將contentFor設置爲模板,以便您可以將事件分配給該模板。

{{> contentFor region="anotherDomRegion" template="anotherDomRegion"}} 

然後;

<template name="anotherDomRegion"> ... </template> 

and;

Template.anotherDomRegion.events({ ... }); 

祝你好運!

相關問題