0

I'm trying to build a helper w/ an else block.

I can only find handlebars examples. They show that options.inverse is a function, but for my HTMLBars helper the options.inverse is an object. (e.g not callable)

template code:

  {{#can 'audit' 'user' }}
    AUDIT_LINK_PLACEHOLDER
  {{else}}
    NO_AUDIT_LINK
  {{/can}}

helper code:

Ember.Handlebars.registerHelper("can", function(verb, noun, options) {
  var abilities =  this.container.lookup('controller:application').currentAbilities;
  var sub_ability = abilities[noun] || {};
  var fn = (sub_ability[verb] || sub_ability['manage']) ? options.fn : options.inverse;
  return fn(this);
});
hewsonism
  • 424
  • 4
  • 11

1 Answers1

2

Normally you would inject your user into all controllers, and the user would have the property canAudit:

App.User = DS.Model.extend({
  canAudit: DS.attr('boolean')
});

And your template would look like this:

{{#if user.canAudit}}
  [ ... ]
{{else}}
  [ ... ]
{{/if}}

The most basic way to do this, if you don't want to deal with injection, and already have the user instance on your application controller, is to do this:

App.YourController = Ember.Controller.extend({
  needs: ['application'],
  user: Ember.computed.alias('controllers.application.user')
});

After setting this up, the user will be accessible inside your controller.

You can find everything you need here, depending on how you want to solve it:

http://emberjs.com/api/classes/Ember.Controller.html#property_needs http://emberjs.com/api/#method_computed_alias

http://emberjs.com/guides/understanding-ember/dependency-injection-and-service-lookup/

Martin
  • 2,302
  • 2
  • 30
  • 42
  • Thanks for the response. I need a solution that supports N actions on M objects. In my example i'm asking if the currentuser has the ability the 'audit' action on the 'user' object or has the 'manage' ability on the 'user' object (in other words can this system user audit other users of the system). I guess this would lead to programmatically generating (N + 1) * M authorization methods for the User model For what its worth, in my example above, the true case works fine. Its the inverse (the else block) that doesn't work. – hewsonism Mar 04 '15 at 23:04
  • Ah! Perhaps you can use a subexpression, which looks like this: {{#if (can 'audit' 'user')}}. An ember-cli helper would look like this: export default Ember.Handlebars.makeBoundHelper(function(value1, value2) { ... }); – Martin Mar 04 '15 at 23:11