SSPUG retrospective: Creating Knockout user experiences in SharePoint with JavaScript
I had an absolute blast presenting Knockout, AJAX, jQuery, SP2010 REST services, and spservices tonight at the February 2012 Sydney SharePoint user group.
The turn out was awesome, and I want to thank you guys for the great questions and the awesome attention for listening me talk about this stuff, with great excitement, for 1.5 hours.
This is what we built, not bad, in 90 minutes ;-)
Great questions:
Why do you need to call ko.utils.unwrapObservable for the key function?
This is actually pretty important, and I didn't explain it very well until I thought about it afterwards.
The reason is that I believe Knockout is actually building a dependency graph when you use knockout expressions and compute functions. If you use data.Id() - you will actually set up a dependency chain between the knockout viewmodel and the key function. I can't imagine it would be disastrously bad - since you don't bind the key function to the UI, but it is an unnecessary performance hit.
What do you do with the formatting?
The easiest way, I think, is to build a compute function and then return the value you want in the right format you wanted it. Knockout itself doesn't contain formatting functions, and I think it will probably be implemented as an separate plugin.
ASP.NET's built in JavaScript, which is available in SharePoint, contains a lot of formatting functions for rendering both currency and dates.
A separate, slightly prettier technique, is to add additional binding handlers (aka, the text:, html:, value: expressions), and create a new binding expression that also does formatting. In my projects, I've used
- money: MyCurrency
- date: MyDate
These are one-way binding, because I'm lazy, but you can make them two-way as well if you want to handle the parsing.
Downloads
- https://static1.squarespace.com/static/5527bff2e4b0b430660b0d10/5527c30de4b030eeeef09715/5527c30ee4b030eeeef09ae6/1329820720137/Creating+Knockout+User+Experiences+in+SharePoint+with+JavaScript_sspug201202.pptx
- https://static1.squarespace.com/static/5527bff2e4b0b430660b0d10/5527c30de4b030eeeef09715/5527c30ee4b030eeeef09ae7/1329820714717/knockout-rest-sspug-201202.zip
Next Presentations
No idea, but I plan to be presenting this for the rest of the year in the various SharePoint Saturdays, so bring your colleagues for a exciting session on a very cool, new technique.