Created
September 11, 2012 10:35
-
-
Save ppcano/3697504 to your computer and use it in GitHub Desktop.
Handlebar helper to comparte ember objects
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
| var getBindingValue = function(context, options, path) { | |
| var normalized = Ember.Handlebars.normalizePath(null, path, options.data), | |
| thisContext; | |
| if (normalized.isKeyword) { | |
| thisContext = normalized.root; | |
| } else if (!Ember.isGlobalPath(path)) { | |
| thisContext = context; | |
| } else { | |
| thisContext = null; | |
| } | |
| return Em.Handlebars.getPath(thisContext, normalized.path); | |
| }; | |
| Handlebars.registerHelper('if_eq', function(context, options) { | |
| var values = context.split(' '), | |
| value = getBindingValue(this, options, values[0]), | |
| expected = getBindingValue(this, options, values[1]); | |
| return (value === expected) ? options.fn(this) : options.inverse(this); | |
| }); | |
| Handlebars.registerHelper('unless_eq', function(context, options) { | |
| var values = context.split(' '), | |
| value = getBindingValue(this, options, values[0]), | |
| expected = getBindingValue(this, options, values[1]); | |
| return (value !== expected) ? options.fn(this) : options.inverse(this); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Could you please provide a jsfiddle with actual data to follow the code easily?