2015-04-17 88 views
1

There is a library that allows you to attach helpers to collections像这样:应用帮手所有集合的MongoDB /流星

Meteor.users.helpers({ 
    profile: function() { 
    return Profiles.findOne(this.profileId); 
    } 
}); 

这个伟大的工程为建立集合,但我想打,默认情况下适用于以往单个集合在我的数据库“一般”的帮手。

EveryCollection.helpers({ 
    fullName: function() { 
    var schema = this.simpleSchema()._schema; 
    if(hasKey(schema, 'firstName') && hasKey(schema, 'lastName')) { 
     return getKey(this, 'firstName') + " " + getKey(this, 'lastName'); 
    } 
    } 
}); 

这样一来,我可以这样做:

> Admins.findOne().fullName(); 
"Cat Woman" 
> Meteor.users.findOne().fullName(); 
"Bat Man" 

这将是,默认情况下,添加到每个集合作为一个帮手。这可能吗?我想它需要添加一些东西到prototype,但无法找到合适的项目添加到。

编辑:比如说较差,想要的东西,在一个实例变量

回答