2015-04-07 72 views
1

爲什麼這不起作用?流星空間條不能訪問父上下文

Template.tests.helpers 
    test_value: -> "Test" 
    contacts: -> Contacts.find() 
    contact: -> Contacts.findOne() 

實施例1:

<template name="tests"> 
    {{test_value}} 
    {{#with contact}} 
      <p>{{firstname}}</p> 
      <p>{{../test_value}}</p> 
    {{/with}} 
</template> 

../test_value內部觸點不呈現

實施例2

<template name="tests"> 
    {{>testContact}} 
</template> 

<template name="testContact"> 
    {{test_value}} 
    {{#with contact}} 
     <p>{{firstname}}</p> 
     <p>{{../test_value}}</p> 
    {{/with}} 
</template> 

沒有什麼工作在這個例子。我假設如果在testContact模板中沒有定義數據上下文,它將繼承他父級的數據上下文。

實施例3

<template name="tests"> 
    {{>testContact}} 
</template> 

<template name="testContact"> 
    {{test_value}} 
    {{#with ../contact}} 
     <p>{{firstname}}</p> 
     <p>{{../test_value}}</p> 
    {{/with}} 
</template> 

也不起作用。

這讓我瘋狂!

EDIT

實施例4

<template name="tests"> 
    {{#with contact}} 
     {{> testContact}} 
    {{/with}} 
</template> 

<template name="testContact"> 
    <p>{{firstname}}</p> 
    <p>{{../test_value}}</p> 
    </p> 
</template> 

在這裏,我可以看到,數據上下文是在testContact模板接觸。然後我會期待{{../test_value}}工作,因爲test_value與我的幫助程序中的聯繫人級別相同,但不會。

實施例5

<template name="tests"> 
    {{#each contacts}} 
     {{> testContact}} 
    {{/each}} 
</template> 

<template name="testContact"> 
    <p>{{firstname}}</p> 
    <p>{{../test_value}}</p> 

</template> 

回答

1

嘗試實施例1不使用../測試值之前。

助手與數據上下文不一樣,這就是爲什麼示例2和3不起作用。

如果測試值幫助函數取決於父級上下文,那麼您必須使用../將參數傳遞給測試值助手。

+0

謝謝你的回答!這是否意味着我應該只使用助手來查看屏幕上的內容並使用其他內容,比如鐵路路由器來定義我的數據上下文? – ndemoreau

+0

使用助手無論你想要的。您可以通過編寫{{> testcontact data = data testvalue = testvalue}}將幫助者的數據傳遞給子模板。 –

+0

對不起再次打擾您,但是什麼時候可以使用../然後?我在上面的問題中創建了一個示例4。這是一個很好的例子,我期望它能夠工作,但它不會... – ndemoreau