Reputation: 166
I was going through the codebase of an old project that uses Ember-CLI 1.13 and found something strange.
There are many helpers that are not directly used inside templates but are used in component js files by importing into them. One such example is
//..helpers/my-helper.js
export function func1 (param1, param2) {
//return something;
}
export function func2 (param1, param2) {
//return something;
}
export function func3 (param1, param2) {
//return something;
}
export default Ember.Helper.helper(func1);
export default Ember.Helper.helper(func2);
export default Ember.Helper.helper(func3);
And inside a component js file, I could see the above helpers being imported and used.
//../components/my-component.js
import Ember from "ember";
import { func1 } from '../helpers/my-helper';
import { func2 } from '../helpers/my-helper';
import { func3 } from '../helpers/my-helper';
I have few questions:
Upvotes: 0
Views: 318
Reputation: 5991
Shouldn't we create a utility instead of a helper in this case?
Yes, but sometimes programmers are lazy or very restricted in time (even though moving function to utilities doesn't look like time-consuming task)
Is it ok to include many functions in a single helper file?
Yes, it is fine to have many functions in file and export them, but as far as I know, only default
export will work in templates as helper. And I am 99% sure that not having default export will lead to build error.
Are the imports inside the component file necessary?
If these imports are used in component's code, then they are necessary. Otherwise, no.
Upvotes: 3