Created
April 15, 2016 13:09
-
-
Save jltafarel/bef5b0499352cca633dfccba0ace67c8 to your computer and use it in GitHub Desktop.
Handlebars helper to use conditional operators.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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