2013-03-26 192 views
1

我有一個從XML轉換,並保持「@屬性」一個JSON得到JSON @屬性項:如何在流星模板

{"stop":"1021","route":"0057","direction":"1","departures":{"departure":[ 
{"@attributes":{"accurate":"1","headsign":"Rennes R"},"content":"2013-03-25T12:00:23+01:00"}, 
{"@attributes":{"accurate":"0","headsign":"Rennes R"},"content":"2013-03-25T12:20:00+01:00"}, 
{"@attributes":{"accurate":"0","headsign":"Rennes R"},"content":"2013-03-25T12:40:00+01:00"}]}},... 

你可以在這裏訪問的JavaScript性能[1]與:

departure[0]["@attributes"].accurate 

但如果你想在流星的模板中抓住它,你怎麼做到這一點?

[1] JSON @attributes

+1

我有張貼我自己的答案:(之前等待7小時 - >解決方案是在這裏:http://bit.ly/YstER0 – 2013-03-26 09:06:05

+1

也許最好不要縮短網址並提供一些披露,你鏈接到自己的網站 – Swadq 2013-03-26 10:45:24

+1

@Swadq評論是有限的長度,我會把全部內容作爲答案在這裏1小時;) – 2013-03-26 16:05:33

回答

0

我建議訪問的功能領域,如

Templates.your_template.attributes = function() 
{ 
    return this["@attributes"]; 
} 

在你的模板

{{attributes}} 
+0

thx adrianj98,3或2鬍鬚?我會檢查{{{attributes}}}。精度是否正常 – 2013-04-14 15:49:41

0

模板中的HTML側面看起來像這樣:

<template name="status"> 
    <div class="line {{status}}_line"> 
     {{stop}} 
     {{#each departures.departure}} 
     {{> attributes}} 
     {{/each}} 
    </div> 
</template> 

<template name="attributes"> 
    <div class="attributes"> 
     {{content}} 
     accurate : {{{access_attributes this}}} 
    </div> 
</template> 

,並在JS方面:

Template.attributes.access_attributes = function(context) { 
    return context["@attributes"].accurate 
}