hewsonism
hewsonism

Reputation: 424

HTMLBars helper with inverse

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);
});

Upvotes: 0

Views: 524

Answers (1)

Martin
Martin

Reputation: 2300

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/

Upvotes: 2

Related Questions