Skip to content

Instantly share code, notes, and snippets.

@jltafarel
Created April 15, 2016 13:09
Show Gist options
  • Select an option

  • Save jltafarel/bef5b0499352cca633dfccba0ace67c8 to your computer and use it in GitHub Desktop.

Select an option

Save jltafarel/bef5b0499352cca633dfccba0ace67c8 to your computer and use it in GitHub Desktop.
Handlebars helper to use conditional operators.
import Ember from 'ember';
export function conditional(params/*, hash*/) {
if (arguments.length > 3) {
throw new Error("Handlerbars Helper 'conditional' needs 3 parameters");
}
var first = params[0],
operator = params[1],
last = params[2];
var conditions = {
'==': function() {return first == last },
'===': function() {return first === last },
'<=': function() {return first <= last },
'>=': function() {return first >= last },
'<': function() {return first < last },
'>': function() {return first > last },
'&&': function() {return first && last },
'||': function() {return first || last },
'!=': function() {return first || last },
'!==': function() {return first || last }
}
if (!conditions[operator]) {
throw new Error("Handlerbars Helper 'conditional' doesn't know the operator " + operator);
}
return conditions[operator]();
}
export default Ember.Helper.helper(conditional);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment