在火焰中访问任意键

时间:2014-11-09 22:55:09

标签: meteor meteor-blaze

我需要访问Meteor通常不允许的对象的键,例如以数字开头的键,尤其是GUID。

我最初尝试的是:

<input type="checkbox" checked={{property.62f53e2e-e4d3-4ff1-b451-2325a6bfbfd0}} />

...但是会引发Expected identifier, number, string, boolean, or null

接下来我尝试了JavaScript风格:

<input type="checkbox" checked={{property['62f53e2e-e4d3-4ff1-b451-2325a6bfbfd0']}} />

...这会引发Exception from Tracker recompute function: Can't call non-function: [object Object]

有没有干净的方式来访问这些属性?

2 个答案:

答案 0 :(得分:2)

假设您通过模板数据传递对象,那么.js文件中的辅助函数呢?

Template.someTemplate.helpers({
    property : function()
    {
        return this.property['62f53e2e-e4d3-4ff1-b451-2325a6bfbfd0'];
    }
});

(编辑:那么您只需将{{property}}放入模板中)

答案 1 :(得分:0)

正如@Kyll已经描述的那样,只有帮助者才能真正做到这一点现在(刚刚提交了一个错误/功能请求on GitHub)。

如果您(由于某种原因)想要一个共同帮助者,您也可以使用它:

objectProperty: (object, propertyName) -> object[propertyName]

...并将其放入您的模板中:

{{objectProperty property '62f53e2e-e4d3-4ff1-b451-2325a6bfbfd0'}}