Last weekend, I was invited to talk about Knockout.js at Twin Cities Code Camp. It was a very enjoyable day, as I saw several great presentations and had a chance to talk to many Knockout users.
I was told that there had been several sessions in the past that touched on Knockout, so for this talk I decided to focus on some tips, tricks, and techniques that can be used to get the most out of Knockout. The topics covered included:
- Brief introduction to Knockout
- Getting data in and out
- Extending Knockout’s structures
- Custom Bindings
- Performance tips
There was a small amount of overlap with the session that I did at ThatConference in August, but all of the samples were new for this talk. The project that I used to give the presentation is also described in the ThatConference post and a reference project lives here.
Here is a screencast of the presentation:
Getting the Most Out of Knockout.js - 10/06/2012 - Twin Cities Code Camp
A few clarifications from the presentation:
- At the end of the “getting data out” part, I had typed
var meta
instead ofthis.meta
. - In the
maxLength
extender, I used a writeable computed observable. In thewrite
function, I should have written the new value to our observable (original(newValue);
), in addition to checking its length. - In the
enterKey
custom binding, you probably would want to return the result of the actual function call, as when you returntrue
from a handler called by theevent
binding, it will allow the default action to proceed, which can be useful at times. - In the
modal
binding where we add a handler for thehidden
event, you may want to check if what you are dealing with can be written to as an observable (is an observable or writeable computed observable) by callingko.isWriteableObservable
. So, we might want to callko.isWriteableObservable(data)
before doingdata(null)
. - In the
modal
binding, the wrappers to thewith
binding, should really be written withwith
quoted likereturn ko.bindingHandlers['with'].init.apply(this, arguments);
andreturn ko.bindingHandlers['with'].update.apply(this, arguments);
. In IE < 9 the use ofwith
as a property name will cause an error, as it is a JavaScript keyword.
Participating in the code camp was definitely worth the effort of traveling up to the Twin Cities. I would be happy to hear any type of feedback on the presentation. Photo by @JudahGabriel.