Let's start with the constructor :
- "this" is established as a private variable named viewModel which can be accessed by public methods contained in the eventHandlers public object.
- errorMessage, a JsonResult property that must be returned from every post request, is established as an observable property of the ViewModel instance.
- If specified an error callback is established as a subscription event to be fired whenever the errorMessage observable changes.
- jQuery is extended with a serializeObject method that can be used to create post input objects using selectors that yield either a single element (in which case the resulting object contains properties having values for all of the specified element's child elements) or a list of elements (in which case the resulting object contains properties having values for only the selected elements).
Encapsulated in the public eventHandlers object are 3 methods :
- post
- post takes required parameters of url and successCallback, and optional parameters of shouldMap and element.
- shouldMap defaults to false and controls whether the returned JsonResult object is mapped to an equivalent object containing only observables and observableArrays.
- element is used only when shouldMap == true and limits binding to the specified element which defaults to window.document.
- successCallback will be called with two parameters, url and the returned JsonResult object which is mapped when shouldMap == true.
- map
- map calls the private innerMap helper to obtain a mapped object without databinding. See innerMap description below for more information
- unMap
- unMap creates an equivalent POJO from an object consisting solely of observable and observableArray type properties, maintaining object hierarchy and structure
Encapsulated within the private pvt object are 4 methods and one property. The alertOnErrorproperty is retained from the constructor parameter having the same name. The methods are:
- errorHandler
- sets ViewModel errorMessage observable
- maps and databinds the errorObject if mapping was specified
- logs to console
- then either issues alert or calls view errorHandler
- successHandler
Note that successHandler will be called for some server errors. - maps and databinds returned JsonResult object if indicated by post shouldMap parameter
- if error log to console
- if error alert user if constructor parameter alertOnError == true
- if successful call the successCallback if one was passed on the post request
- map
The private map method is called to databind the mapped object - viewModel is the object to which a sub-View Model object will be added as a property
- url is the url that had been used to obtain a model object that will be mapped to the sub-View Model. It is also used to derive the sub-View Model property name.
- model is the object returned from post to the url and which will be mapped to the sub-View Model via a call to pvt.innerMap
- element is optional and the HTML element that will be used in databinding with the sub-ViewModel as can be seen in the call to ko.applyBindings.
- innerMap innerMap is a helper for the public map method which accepts a model object and recursively iterates properties descendant properties while adding those properties and their values to a corresponding View Model object as observables and observableArrays. This View Model object has identical structure and hierarchy as the input model object.
This concludes the series. The full working, zipped Visual Studio solutions may be downloaded from SkyDrive and include both Visual Studio projects and a database creation script. Other-platform developers can still salvage the ViewModel object from the zip file.
As always, feedback is appreciated.
Click here for the VS2012 solution.
Click here for the VS2010 solution.