Freetime is very hard to come by when you have two small children under three but I actually had a couple of hours spare at this time of writing and had set myself the challenge of implementing an ember.js timeago handlebars helper when I came across this example by @jgwhite that does exactly that. The README gives a very good breakdown of how to implement the helper.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
The key to making this work is the notifyPropertyChange method which is a convenience method that calls propertyWillChange and propertyDidChange. Calling both these methods consecutively is actually what happens behind the scenes when you call set() on an object and the combination triggers a notification to all registered obervers that the property has changed.
My handlebars helper is ever so slightly different and possibly worth a brief comment and is listed below:
1 2 3 4 5 6 7
Line 4 adds a new property to the the hash property of the options argument. Handlebars will mix any property in this options hash into the view. You can also set up bindings and other goodness for each property that you add by using the option.hashTypes hash. I am not doing that in this instance but it is worth noting that you can.
Line 6 uses the view helper to insert a new instance of the App.TimeAgoView into the output. It is worth noting that this is the helper that is called by handelbars for normal view declarations like this:
The upshot of all this is that I feel a bit deflated as I was really looking forward to sinking my teeth into this. I guess I have @jgwhite to thank for that.
I now need a new chanllenge…….and something that I have actually written to blog about…..