Meteor-handlebar-helpers
Meteor-handlebar-helpers copied to clipboard
Make $and and $or take infinite amount of parameters?
UI.registerHelper('$and', function () {
var args = _.toArray(arguments);
if (!args.length) {
return false;
}
var test = args[0];
if (args.length > 1) {
_.each(_.rest(args), function (arg) {
test = test && arg;
});
}
return test;
});
UI.registerHelper('$or', function () {
var args = _.toArray(arguments);
if (!args.length) {
return false;
}
var test = args[0];
if (args.length > 1) {
_.each(_.rest(args), function (arg) {
test = test || arg;
});
}
return test;
});
Is this a good idea? Is it done right/efficiently?
I went for two in binary helpers - but we could have this in the $in/$nin (they currently only support 4)
I normally either convert into array var args = _.toArray(arguments); or use a for loop since its possible to break from it. (I'm a bit old fashion when it comes to array like objects)
The reason for the limited args of two in binary operators is to reason about stuff like:
{{# if $and a $or b c }}
At some point I think spacebars will support something like:
{{#if a && (b || c) }}
Thinking about this some more - not sure how this actually work
Spacebars now (Meteor 1.2) supports nested helpers. Not as succinct as @raix hoped it would be (no native javascript expressions/operators) but we can nest handlebars-helpers
{{#if $and a ($or b ($and c d) ) }}
@rclai do you think we should still enhance these helpers to accept and process arbitrary number of arguments?
This would still be confusing if used in a regular block helper cascade where helpers are evaluated from right to left.
To me, the current way it works with two arguments and the new nesting syntax is just fine for a blanket solution helper like this and anyone in need for something far mre precise could create a dedicated one.
I'm so glad that we have nested expressions now.
I personally wouldn't go overboard in trying to list so many conditionals, but I'm sure someone has tried to but couldn't.
So I think it's still a good idea, especially since we now have nested expressions.
Just try it out, I already wrote the code in the first post.
Thanks. Would you care to do a PR?
@raix what do you think?
:+1: makes perfect sense (please use es6, api.versionsFrom('1.2'); and api.use('ecmascript');)
Hi guys,
it's been over a year, but oh-yeah I am still on blaze and love this package here. Just stumbled upon the useCase where I need to check something like $and condition1 condition2 ($or condition3 condition4).
What is the state on this? Any change of getting this to work and release a new version?
I'll take a pr and do a release if you are up to it :)