XYZ
XYZ

Reputation: 27407

How to use mixin inside custom helper?

I have defined some helper functions inside a mixin; however, I am not sure how to uses these functions inside a custom helper.

I have referred to question below but seems ember no longer have Mixin.apply method.

Accessing an Ember.Mixin within a custom handlebars helper

Upvotes: 0

Views: 285

Answers (2)

XYZ
XYZ

Reputation: 27407

The default helper cannot use mixin because it is stateless.

However, if you need a helper that interacts with your application. You can do so by making a class-based helper.

https://guides.emberjs.com/v2.11.0/templates/writing-helpers/#toc_class-based-helpers

Code snippet below is a class-based helper,

import Ember from 'ember';

export default Ember.Helper.extend({
  compute([value, ...rest], hash) {
    let dollars = Math.floor(value / 100);
    let cents = value % 100;
    let sign = hash.sign === undefined ? '$' : hash.sign;

    if (cents.toString().length === 1) { cents = '0' + cents; }
    return `${sign}${dollars}.${cents}`;
  }
});

Upvotes: 0

Ember Freak
Ember Freak

Reputation: 12872

If you got class based helper, then you can use Mixin as usual.

export default Ember.Helper.extend(YourMixinName,{
  session: Ember.inject.service(),
  onNewUser: Ember.observer('session.currentUser', function() {
    this.recompute();
  }),
  compute() {
    return this.get('session.currentUser.email');
  }
});

Upvotes: 1

Related Questions