Hello,
I would like to pass a parameter to helper and access the properties of the returned object by key in blaze.
My helper looks like:
teamById: function(teamId){ console.log('TEAM ID', teamId); return TeamsCollection.findOne({'_id': teamId}); }
What is working
{{teamById teamId}}
and
{{teamById.name}}
In first case the parameter is transmitted and I see the value in the console. In the second case I get the teams name.
What is not working is the combination:
{{teamById.name teamId}}
and in the console output it states undefined for the teamId value
Spacebars doesn’t support this type of syntax directly. You could wire something similar up using nested sub-expressions though:
test.html:
<body>
{{teamName (teamById 123)}}
</body>
test.js:
Template.body.helpers({
teamById(teamId) {
return {
_id: '123',
name: 'Some Team'
};
},
teamName() {
return this.name;
}
});
That being said, this approach isn’t that flexible. You’re probably better off just sticking with:
{{#with (teamById 123)}}
{{name}}
{{/with}}
Thanks. I was not not familiar with the fact that you can not combine the blaze syntax in that way. I ended up with helper returning object or its value dependent on passed parameter.
teamById: function(teamId, param){
var team = TeamsCollection.findOne({'_id': teamId});
if (param && param in team){
return team[param];
}
else {
return team;
}