Last active
August 29, 2015 13:57
-
-
Save slindberg/9924116 to your computer and use it in GitHub Desktop.
Bound conditional Handlebars helpers for Ember
This file contains 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 ifConditionHelper from 'myapp/helpers/if-condition'; | |
/** | |
* Logical AND Existence Conditional Block | |
* | |
* Usage: {{#if-all-exists field1 field2}}Either field1 or field2 is truthy{{/if-all-exists}} | |
* | |
* Executes the given block if all arguments are defined | |
*/ | |
export default function() { | |
var options = arguments[arguments.length - 1]; | |
options.conditional = function(results) { | |
return results.every(exists); | |
}; | |
return ifConditionHelper.apply(this, arguments); | |
} | |
function exists(value) { | |
return value !== undefined; | |
} |
This file contains 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 ifConditionHelper from 'myapp/helpers/if-condition'; | |
/** | |
* Logical AND Conditional Block | |
* | |
* Usage: {{#if-all field1 field2}}Both field1 and field2 are truthy{{/if-all}} | |
* | |
* Executes the given block if all arguments are truthy | |
*/ | |
export default function() { | |
var options = arguments[arguments.length - 1]; | |
options.conditional = function(results) { | |
return results.every(identity); | |
}; | |
return ifConditionHelper.apply(this, arguments); | |
} | |
function identity(value) { | |
return value; | |
} |
This file contains 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 ifConditionHelper from 'myapp/helpers/if-condition'; | |
/** | |
* Logical OR Existence Conditional Block | |
* | |
* Usage: {{#if-any-exists field1 field2}}Either field1 or field2 is truthy{{/if-any-exists}} | |
* | |
* Executes the given block if any argument is defined | |
*/ | |
export default function() { | |
var options = arguments[arguments.length - 1]; | |
options.conditional = function(results) { | |
return results.any(exists); | |
}; | |
return ifConditionHelper.apply(this, arguments); | |
} | |
function exists(value) { | |
return value !== undefined; | |
} |
This file contains 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 ifConditionHelper from 'myapp/helpers/if-condition'; | |
/** | |
* Logical OR Conditional Block | |
* | |
* Usage: {{#if-any field1 field2}}Either field1 or field2 is truthy{{/if-any}} | |
* | |
* Executes the given block if any argument is truthy | |
*/ | |
export default function() { | |
var options = arguments[arguments.length - 1]; | |
options.conditional = function(results) { | |
return results.any(identity); | |
}; | |
return ifConditionHelper.apply(this, arguments); | |
} | |
function identity(value) { | |
return value; | |
} |
This file contains 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
/** | |
* Bound Conditional if/else Block | |
* | |
* Executes the given block if all arguments are equal | |
* NOTE: this helper is meant to be used by other helpers by specifying | |
* a callback as the `conditional` option | |
*/ | |
export default function() { | |
var args = [].slice.call(arguments); | |
var options = args.pop(); | |
var context = (options.contexts && options.contexts[0]) || this; | |
if (!options.conditional) { | |
throw new Error("A conditional callback must be specified when using the if-condition helper"); | |
} | |
// Gather all bound property names to pass in order to observe them | |
var properties = options.types.reduce(function(results, type, index) { | |
if (type === 'ID') { | |
results.push(args[index]); | |
} | |
return results; | |
}, []); | |
// Resolve actual values for all params to pass to the conditional callback | |
var normalizer = function() { | |
return Ember.Handlebars.resolveParams(context, args, options); | |
}; | |
// This effectively makes the helper a bound helper | |
// NOTE: 'content' path is used so that multiple properties can be bound to using the `childProperties` argument, | |
// however this means that it can only be used with a controller that proxies values to the 'content' property | |
return Ember.Handlebars.bind.call(context, 'content', options, true, options.conditional, normalizer, properties); | |
} |
This file contains 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 ifConditionHelper from 'myapp/helpers/if-condition'; | |
/** | |
* Equality Comparison Conditional Block | |
* | |
* Usage: {{#if-equal field1 field2 "foo"}}field1 and field2 are equal to 'foo'{{/if-equal}} | |
* | |
* Executes the given block if all arguments are equal | |
*/ | |
export default function() { | |
var options = arguments[arguments.length - 1]; | |
// Find all unique values in the array; if one is left, they were all equal | |
options.conditional = function(results) { | |
return results.uniq().length === 1; | |
}; | |
return ifConditionHelper.apply(this, arguments); | |
} |
I'm getting this error in Ember 1.11
TypeError: Ember.default.Handlebars.bind is undefined
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
any word on this? I'm totally stuck trying to do something like {{#if has-permission "myPermission" }}.. the cli docs dont say how to do it, I get errors maybe due to handlebars 2.0.. no idea.. stuck