Meteor.js中的UI.dynamic模板的助手
在Meteor v0.8.2中,似乎必须为dynamic模板调用的各个模板( Template.story_en
, Template.story_ne
)创build助手。
是否可以为dynamic模板( Template.story
)创build助手,并避免为dynamic模板可以使用的所有可能的模板重复使用,如下面的示例中所示? 看来,我正在使用的方法需要大量重复的代码。
story.html
<template name="story"> {{> UI.dynamic template=storyTemplate}} </template>
story.js
Template.story.storyTemplate = function() { return "story_" + Session.get('lang') } // This does not work Template.story.color = function() { return '#f00' } // This works Template.story_en.color = function() { return '#f00' } // This works (but seems to be unnecessary code) Template.story_ne.color = function() { return '#f00' }
您可以使用全局帮助器,或者将帮助器作为数据传递
使用全球帮手(在每个模板上工作)
UI.registerHelper("color", function() { return '#f00' });
或者作为数据传递助手(在当前版本的铁路路由器下不工作 – 打开bug )。
Template.story.helpers({ dataHelpers: function() { var data = UI._templateInstance().data || {}; //Add the helpers onto the existing data (if any) _(data).extend({ color: function() { return "#f00"; } }); return data; }); });
然后html:
<template name="story"> {{> UI.dynamic template=storyTemplate data=dataHelpers}} </template>
然后在子模板中,您可以使用{{color}}
而不需要帮助者。
如果你有铁路路由器的问题,你也可以尝试使用this
而不是UI._remplateInstance.data
。