The first step might look like this:
getField = function(collection, field) {
var projection = {};
projection[field] = 1;
return collection.find({}, {fields: projection});
}
UI.registerHelper("getField", getField);
Which you would call in your templates like {{getField myCollection "q1"}}
, together with a helper returning myCollection
. You could take it further:
var makeFieldGetter = function(collection, field) {
return function() {
return getField(collection, field);
};
};
Template.whatever.helpers({
gimmeResults1: makeFieldGetter(myCollection, "q1")
gimmeResults2: makeFieldGetter(myCollection, "q2")
});
Or even further:
var makeFieldGetters = function(collection, fields) {
var obj = {};
fields.forEach(function(field) {
obj["gimme_" + field] = makeFieldGetter(collection, field);
});
return obj;
};
Template.whatever.helpers(makeFieldGetters(myCollection, ["q1", "q2"]));
// creates helpers gimme_q1, gimme_q2