Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ember.js helper with moment.js (using ember-cli) : Handlebars error: Could not find property

I'm trying to use moment.js in my ember.js app (built with ember-cli), I have a trouble with this error

Handlebars error: Could not find property 'formatDate' on object

I think it's same as this error How to use Custom helpers in ember-app-kit? but I already did the same approach but not working yet. Anyone got same error? Please help me to figure out.

I put app.import('vendor/momentjs/moment.js'); in Brocfile.js and "moment": true in .jshintrc as in ember-cli documentation, and I used the helper {{formatDate date}} in PostsTemplate

I created a helper app/helpers/formatDate.js

var formatDate = Ember.Handlebars.makeBoundHelper(function(date) {
  return moment(date).fromNow();
});

export default formatDate;

I also tried this syntax in app/helpers/formatDate.js, but neither works and both get same error

export default Ember.Handlebars.registerBoundHelper('formatDate',function(date) {
  return moment(date).fromNow();
});
like image 793
Yuichi Avatar asked May 28 '14 03:05

Yuichi


2 Answers

I think your file name 'formatDate.js' has the wrong format. Try 'format-date.js' and it should work.

Excerpt from http://iamstef.net/ember-cli/:

Handlebars helpers will only be found automatically by the resolver if their name contains a dash (reverse-word, translate-text, etc.) This is the result of a choice that was made in Ember, to help both disambiguate properties from helpers, and to mitigate the performance hit of helper resolution for all bindings.

Use your new 'format-date' helper like this:

{{format-date "29/05/2014"}}
like image 152
Steven Avatar answered Oct 19 '22 16:10

Steven


I ran into this symptom as well and had a different solution.

I had a helper in app/helpers/fh.js called 'fh' in order to be able to use it I needed to add it to the controller as follows

import fh from '../helpers/fh';

If I didn't have the import line I would get the following error:

"Handlebars error: Could not find property 'fh' on object"

like image 44
sshevlyagin Avatar answered Oct 19 '22 16:10

sshevlyagin