For either of the JS Bins, you can walk through the snapshots to see my progress.
1) Create a placeholder div for your component
The choice of data-component to identify the component and data-attrs to pass in data
are our own interface for linking the template to the Component. You can use other
attributes, as you’ll see below.
2) Link to source files
Though I’m using ember.debug here, once you’ve debugged your code, you’ll want to use ember.prod.
We need the ember-template-compiler because we’re not pre-rendering our templates, but getting them
from the DOM.
To improve usability of the JS Bin, it’s a good idea to lock down the script versions in snapshots, as above.
To work on the edge, you may want to develop on the latest release or canary, e.g.
3) Make your Ember App
If you want to get fancy and making debugging your JS Bin a bit easier, instead:
3) Create an Ember Component
Here’s my handlebars template, right in the DOM. I’m going to pass it a title and rows.
4) Make sure you specify the layoutName to match the data-template-name of your handlebars template
Here, it’s components/doc-printer. The component name must have a hyphen (-) in it.
The doc-printer component maps to App.DocPrinterComponent.
5) Use jQuery to replace your html div with your Ember component
Now there’s a lot going on in there, but here’s the gist of it:
Find the handlebars template for the component in the DOM.
Optional: Get its data-attrs, a convenient way to pass data, as JSON, from the template into the component.
Optional: Find the component object. If you know what it is, you can call create on it directly.
Create an instance of the component and append it to the DOM.
Appendix
That wasn’t so terrible, right? Now, there’s a bunch of boiler-plate in there.
First, let’s make the placeholder. We’re just using a class attribute here to identify it.
Then, write our template. Notice that this template binds an action.
Define our App and Component. Notice that I’m now using Ember.$ in place of $.
And, this time, let’s hook into ember initializers:
Both the App.initializer and App.instanceInitializer need a name, but it doesn’t
seem to matter what it is, so I picked obviously ridiculous names.
In the App.initializer, I’m pausing the App initialization until Ember.$(document).ready() fires.
Once, the document is ready, iterate for all components, here .component-placeholder but
could just as easily be [data-component] for a more general solution.
I’ve left some of my debugging comments in the code for areas where I had issues.
Epilogue
I hope that helps you get started with just dropping Ember in your static site.