Gabe Rainbow
Gabe Rainbow

Reputation: 3738

ember cli multiple defs inside single file and breaking naming conventions

Is there any (tricky, dorky, or otherwise frowned upon) way of placing many definitions in a single helper file...

For example, I have the following helpers that dont seem to warrant a file per each.

Ember.Handlebars.helper('format-markdown', function(input) {
  return new Handlebars.SafeString(showdown.makeHtml(input));
});
Ember.Handlebars.helper('format-fromnow', function(date) {
  return moment(date).fromNow();
});
Ember.Handlebars.helper('format-long', function(date) {
  return moment(date).format('MMMM Do YYYY, h:mm:ss a');
});
Ember.Handlebars.helper('format-currency', function(mynumber) {
  return numeral(mynumber).format('$0,0.00');
});

im porting from a ember proj where everything is lumped into grossly too few files.

ach. i found an example of it in the docs. however its not clear where the import goes.

 1 // helpers/custom-helpers.js
 2 var customHelpers = function() {
 3   Ember.Test.registerHelper('myGreatHelper', function (app) {
 4     //do awesome test stuff
 5   });
 6 
 7   Ember.Test.registerAsyncHelper('myGreatAsyncHelper', function (app) {
 8     //do awesome test stuff
 9   });
10 }();
11 
12 export default customHelpers;

is startApp a special entrypoint. This does not appear to work. 1 // helpers/start-app.js 2 import customHelpers from './custom-helpers'; 3 4 export default function startApp(attrs) { 5 //...

Upvotes: 0

Views: 464

Answers (1)

skylar
skylar

Reputation: 31

You can simply register the helpers as you did before in file outside ofthe helpers directory.

They can be registered as:

Em.Handlebars.registerBoundHelper('make-plural', function(word) {...})

[Note that all helpers should now have dashed names as an efficiency for handlebars processing.]

I put my helpers in a file in app/utils and imported it from app.js at the top. This keeps app.js clean, but puts all the tiny helpers in a single file in a sensible place where they are easy to manage.

The auto-registration of helpers by convention is just a feature of EmberCLI you can use or ignore.

Upvotes: 0

Related Questions