I want to create a library containing general-purpose javascript functions and classes for use in any Ember object in an Ember-cli project. What's the recommended procedure for doing this?
-
You would put them in your /utils directoryPatsy Issa– Patsy Issa2015-12-08 16:08:00 +00:00Commented Dec 8, 2015 at 16:08
-
1Is this for you own project, or for sharing?user663031– user6630312015-12-08 16:36:23 +00:00Commented Dec 8, 2015 at 16:36
-
This is for my own project.user5638246– user56382462015-12-08 22:51:02 +00:00Commented Dec 8, 2015 at 22:51
Add a comment
|
2 Answers
You should generate new util in Ember CLI:
ember g util your-name
Then you can import it using:
import yourName from '../utils/your-name'
1 Comment
user5638246
Is the from '../utils/your-name' relative to location of the comsumming object? I tried what you suggested before posting this question. I kept getting a "could not find module error." I posted another question regarding this. Please search 34167418. Do you know what's causing the error?
Creating an addon is the Ember-CLI-preferred method of doing that. Addons can be published to NPM and installed in Ember CLI projects using NPM. This makes it incredibly easy to share code. There's a guide for developing addons on the Ember CLI site.
3 Comments
user5638246
But this is just a simple utility function that's of no value to anyone else, so publishing to NPM seems a bit inappropriate.
GJK
I may have misread your question. I thought you wanted a way to share between different Ember CLI projects. If you want a function for just one project, then a utility is the way to go.
user5638246
I can't get import to work no matter what I tried. Please see post 34167418.