Skip to content

Instantly share code, notes, and snippets.

@slindberg
Last active August 29, 2015 13:57
Show Gist options
  • Save slindberg/9924116 to your computer and use it in GitHub Desktop.
Save slindberg/9924116 to your computer and use it in GitHub Desktop.
Bound conditional Handlebars helpers for Ember
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;
}
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;
}
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;
}
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;
}
/**
* 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);
}
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);
}
@injaon
Copy link

injaon commented Jul 23, 2015

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