Dependency versions
This post was built with Ember.js 1.0.0RC1, handlebars RC3, and jQuery 1.9.1
Understand the goal
What is the point?
Recently, I was discussing a mechanism for storing json metadata in an Ember application and that thought experiment led me to this possible solution. We will build a simple jSfiddle that demonstrates a possible solution to this problem that utilizes several interesting Ember-isms including injections, simulated Ajax with the Runloop, and the didInsertElement hook on Ember.Views.
Show me this…fiddle
Feel free to check out this Completed Fiddle or build your own fiddle as we go.
Ember application setup and a custom initializer!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
What the … is all this mess?
Don’t worry this is easy! It just looks messy because you are a hopeless
minimalist obsessing over LOC control. Remember the goal was to inject
a single instance of our new paginationtracker object onto all controllers,
routes, and the store so that we can use the data to affect our displays.
First, we create an app and define our PaginationTracker object by extending Ember.Object.
Second, we create a custom Ember.Application.initializer to handle our
injection. Read more about this awesome functionality at
Ember Application Source
We now have an Ember application with the setup desired…sadly we can see
no proof that my claims are valid. Let’s fix that!
Fake ajax with Ember.run to change paginationtracker
1 2 3 4 5 6 7 | |
Quick explanation
We are using the default template (not named explicitly because Ember will
automagically name it “application” for us). We want to display model1pagination
and model2pagination from an attribute called “pagetracker” (our pagination
tracker singleton that we just injected onto all controllers) on our
application controller.
Finally, we show a little message if our data has changed (just for
fun, though this value is ALSO stored on the paginationtracker).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | |
This is cool…what is it?
We want our view to fire off a “fake ajax-y method” on our controller when it is first inserted into the DOM. The initial pagination values are displayed for 4 seconds before they are replaced by updated values.
Go home point-of-this-article…you’re drunk
The key thing to understand from this code is that we are accessing a singleton
instance of our paginationtracker through our applicationController
thanks to our clever injection!
We can update its properties and then use standard handlebars lookup paths to
access that object to display its current attribute values. This is wonderful
as we could now use these values in any controller, route, or even directly
on the store to make decisions about our application’s behavior.
yay!
Unsolicited opinion
Ember is really not hard once you learn to stop fighting it and instead LEVERAGE its conventions to achieve world domination…like Zuckerberg..but cooler.
Upcoming blogs (omg see the future)
I want to explore some more advanced Ember use-cases including undo/redo, animation, and extracting view attributes from CSS stylesheets (black.fuckin.magic). As always, follow @stv_kn for updates on stuff and links to useful fiddles.