Ember: Moving from Ember sprinkles to Ember CLI. Part 2
Tweet Follow @hazula
Following ember-sprinkling-in-components
Note, this was written using Ember 1.13.0.
Why?
Ember rendering Component
- Like web components
- Like react components
- Just Ember without a router
- Just Ember not a single page app (SPA)
Can make just an HTML page with one or more Ember apps on it (via rootNode)
Acknowledgements
Mitch Lloyd paired with me to bootstrap and write the core work of setting up an ember-cli components-only app, and for his work in writing Ember-Islands.
Many thanks as well to many others in the embercommunity slack for their assistance.
Getting Started
You’ll want to install npm, bower, and ember-cli and use a recent version of Node.js. Here’s what I did (on OSX):
Basic steps
An Ember CLI app for rendering Ember Components outside of an Ember app.
-
Create and enter the directory for your Ember app:
mkdir _ember; cd _ember
. -
Pick a name for your Ember app, and create it:
ember init --name=viewtastic
(orember init --dry-run --name=viewtastic
to preview what files would be added/changed.). -
Generate a Component. Its name must have a hyphen. e.g.
ember g component doc-printer
. -
Config
Brocfile.js
(or ember-cli-build.js as needed/desired). SeeGotchas
below. -
Edit
app/index.html
to add a data component, e.g.<div data-component="doc-printer"></div>
. -
Make it super easy to render Ember Components in a non-Ember app:
ember install ember-islands
. -
Build the Ember app assets:
ember build -e production
. -
Copy the desired assets into your non-Ember app: e.g.
cp dist/assets/{vendor,viewtastic}.js ../assets/
. -
Add the data-component element Ember-Islands is looking for and load the generated assets on the page(s) you want.
-
Profit.
-
Attend to the gotchas, below:
1) We need to disable ‘storeConfigInMeta’.
As the EmberCLI guide states
This meta tag is required for your ember application to function properly.
Because Ember doesn’t control our app’s html, we need to disable ‘storeConfigInMeta’.
[The meta] tag [will] be part of your compiled javascript files
2) We need to turn off ‘fingerprinting’ of our production-built assets.
We don’t want to have to change the script src in our site every time we change our Ember app. Per the EmberCLI guide
enabled
- Default:app.env === 'production'
- Boolean. Enables fingerprinting if true. True by default if current environment is production.
So, in our Brocfile.js
, we have
I’ve also disabled source maps, but this isn’t strictly necessary.
And I’ve also configured ember-cli to build the assets I want to dist/assets/
3) We need to dynamically compile our templates in development / test environments
So, in our Brocfile.js
, we have
We also want to use ember-cli-htmlbars-inline-precompile to test our Component rendering:
Per Ember component integration tests:
Other changes from ember init
package.json
bower.json
References and further reading:
- Original PR where I merged in this code to the blog
- State of the Ember app as of this writing
- Integration tests for components
(This area will be updated)
blog comments powered by Disqus