简体   繁体   中英

Generate Helper handlebars in ember-cli

I have followed the ember guide to create an handlebar helper like in this jsbin

This is the helper

Ember.Handlebars.registerBoundHelper('format-date', function(format, date) {
  return moment(date).format(format);
});

I can successfully reuse in my jsbin but i get an error when i apply the same helper in my application, this is the error Uncaught TypeError: fn.apply is not a function in my helper function , i am not implementing it correctly in my ember-cli

This is the same helper in my application

import Ember from 'ember';

export default Ember.Handlebars.registerBoundHelper('format-date', function(format, date) {
        return moment(date).format(format);
});

What's the problem?

I have also used makeBoundHelper instead of registerBoundHelper

Just to let you know i have generated the ember helper in these steps

1) From command prompt run ember generate helper "format-date"

This was the helper generated

import Ember from 'ember';

export function formatDate(params/*, hash*/) {
  return params;
}

I am definitely mistaking the way i am exporting the format-date helper

How it should be in my ember-cli?

Ps I am still using Ember 1.12.0 , i have seen a similar issue here

I had to update to Ember Version 1.13.7 and change the helper to this:

import Ember from 'ember';

export function formatDate(params) {
  var date = params[0];
  return moment(date).format('LLL');
}

export default Ember.Helper.helper(formatDate);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM