Skip to content

Instantly share code, notes, and snippets.

@digitalplaywright
Last active August 29, 2015 14:00
Show Gist options
  • Save digitalplaywright/11388029 to your computer and use it in GitHub Desktop.
Save digitalplaywright/11388029 to your computer and use it in GitHub Desktop.
Handlebars.registerHelper("can", function(actor, action, object, fn)
{
var context = (fn.contexts && fn.contexts[0]) || this;
var canAction = function(can_args)
{
var rules = context.container.lookup('rules:eval');
return rules.can({activity: can_args[1], actor: can_args[0], object: can_args[2] });
};
var args = [actor, action ,object, fn];
var options = args.pop();
// 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);
};
return Ember.Handlebars.bind.call(context, 'content', options, true, canAction, normalizer, properties );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment