CornerMan
Knockouts faithful servant.
Example
{ CornerManViewModel; thisnameProvider = this;}CornerMan; { ViewModel; thisname = ko;}CornerMan; var helloVM = ;var nameVM = ;helloVM; var app = CornerMan;app;app;
Works with CoffeeScript class too:
: -> super'hello-template' @nameProvider = @childObservable : -> super'name-template' @name = koobservable'World' helloVM = nameVM = helloVMnameProvidernameVM app = CornerMancreateappsetRootViewModelhelloVMappbindRootViewModel
CornerMan API
The CornerMan API is exposed through the global variable CornerMan
.
// ClassesCornerManRouter // Provides URL routing infastructure.CornerManViewModel // Base ViewModelCornerManContentViewModel // Subclass of ViewModel, provides propagated controls CornerManControlViewModel // Subclass of ViewModel, used with the ContentViewModelCornerManEvents // Definition of events dispatched from ViewModel when changes occur // UtilsCornerManinherit // Helper for correctly setting up prototype chain when subclassing.CornerMancreate // Initializes a new CornerMan instance
CornerMan
The global CornerMan
variable is also a class providing shortcuts for setting up the app.
CornerMan(rootViewModel)
rootViewModel
ViewModel (optional): The root ViewModel of the application.
It's easiest to have a single root ViewModel that then has children for the various sections of the application.
{ CornerManViewModel;}CornerMan; var rootViewModel = ;var app = rootViewModel // or CornerMan.create(rootViewModel)app;
getRootViewModel()
=>
ViewModel: Returns the root ViewModel or null.
setRootViewModel(rootViewModel)
rootViewModel
ViewModel
registerRoute(route, callback [, callback ]...)
route
String: Route to register the callback(s) with.callbacks
Function: Functions called when the URL matches the given route.
Adds a route to the Router associated with this CornerMan instance. For more details, see Router#registerRoute
below.
get(route, callback [, callback ]...)
route
String: Route to register the callback(s) with.callbacks
Function: Functions called when the URL matches the given route.
Alas for #registerRoute
.
listen()
Calls #listen
on the Router associated with this CornerMan instance. For more details, see Router#listen
below.
setTemplateEngine(templateEngine)
templateEngine
ko.nativeTemplateEngine: Subclass of Knockout's template engine. Sets a template engine that will be used for allchild
andchildren
data bindings.
bindRootViewModel(element)
element
Node (optional): DOM element to bind the root ViewModel to.
Binds the root ViewModel to the given element or document.body
if no element is given.
ViewModel API
ViewModel
CornerMan.ViewModel
ViewModel(template)
template
String (optional): Template for the ViewModel.
{ CornerManViewModel;}CornerMan;
Events (CornerMan.Events)
CHILD_ADDED
Dispatched from a ViewModel when a child is added to it.
var viewModel = ;viewModel;
CHILD_MOVED
Dispatched from a ViewModel when a child is moved from one key to another.
var viewModel = ;viewModel;
CHILD_REMOVED
Dispatched from a ViewModel when a child is removed from it.
var viewModel = ;viewModel;
MOVED_KEYS
Dispatched from a ViewModel when the ViewModel moves from one key to another key but without its parent changing.
var viewModel = ;viewModel;
ADDED_TO_PARENT
Dispatched from a ViewModel when the ViewModel is added as a child to another ViewModel.
var viewModel = ;viewModel;
REMOVED_FROM_PARENT
Dispatched from a ViewModel when the ViewModel is removed as a child from another ViewModel.
var viewModel = ;viewModel;
Methods
childObservation(initialValue, options)
initialValue
ViewModeloptions
Objectkey
String (optional): The key to use for this observable
=>
Observable: Returns a Knockout observable
Creates an observable that contains a ViewModel. Any ViewModel set in the observable will be set as a child of this ViewModel.
{ CornerManViewModel; thischild = this;};CornerMan;
childrenObservable(initialValue, options)
initialValue
ViewModeloptions
Objectkey
String (optional): The key to use for this observable
=>
Observable: Returns a Knockout observable
Creates an observable that contains an array of ViewModels. ViewModels added/removed from this observable be set as children of this observable.
{ CornerManViewModel; thischildren = this;};CornerMan; var fooVM = ;
addListener(event, callback)
event
ViewModel.Eventscallback
Function: Refer to ViewModel.Events for callback parameters
Adds a listener to the ViewModel.
removeListener(listener)
listener
Function: The callback function passed to #addListener
Removes the listener from the ViewModel.
getParent()
=>
ViewModel: Returns the parent of this ViewModel or null.
getTemplate()
=>
String: Returns the template of this ViewModel or null.
getKeys()
=>
Array< String >: Returns all of the child keys of this ViewModel.
getKeysObservable()
=>
Observable< Array< String > >: Returns an observable containing all of the child keys of this ViewModel.
getChildren()
=>
Array< ViewModel >: Returns all of the children of this ViewModel.
getChildrenObservable()
=>
Observable< Array< ViewModel > >: Returns an observable containing all of the children of this ViewModel.
getChildrenForKey(key)
key
String=>
Array< ViewModel >: Returns all of the children of this ViewModel at the given key.
getChildrenObservableForKey(key)
key
String=>
Observable< Array< ViewModel > >: Returns an observable containing all of the children of this ViewModel at the given key.
getKeyForChild(viewModel)
viewModel
ViewModel: Child of this ViewModel=>
String: The key of the given ViewModel or null.
addChildAtKey(key, viewModel)
key
StringviewModel
ViewModel: ViewModel to add as a child of this ViewModel.=>
Boolean: Returnstrue
if the ViewModel was added as a child at the given key; returnsfalse
if the ViewModel is already a child at the given key.
If the key is not significant, consider using #addChild
instead.
addChildrenAtKey(key, viewModels)
key
StringviewModels
Array< ViewModel >: ViewModels to add as childlren of this ViewModel.
If the key is not significant, consider using #addChildren instead.
addChild(viewModel)
viewModel
ViewModel: ViewModel to add as a child of this ViewModel.=>
String: Returns the key generated for the new child.
To add a child with a given key, use #addChildAtKey
instead.
addChildren(viewModel)
viewModel
ViewModel: ViewModel to add as a child of this ViewModel.=>
String: Returns the key generated for the new child.
To add childlren with a given key, use #addChildlrenAtKey
instead.
removeChildAtKey(key, viewModel)
key
StringviewModel
ViewModel: Child ViewModel to remove.=>
Boolean: Returnstrue
if the child exists at the given key and was removed from this ViewModel; returnsfalse
otherwise.
removeChild(viewModel)
viewModel
ViewModel: Child ViewModel to remove.=>
Boolean: Returnstrue
if the ViewModel is a child of this ViewModel and was removed; returnsfalse
otherwise.
replaceChildrenAtKey(key, viewModels)
key
StringviewModels
ViewModel: ViewModels to add as children of this ViewModel. Removes all child existing at the given key and then adds all of the given ViewModels at that key.
ViewModel.generateKey([length] [, availableCharacters])
length
String (optional): Length of the key to generate, defaults to 10.availableCharacters
String (optional): String containing all of the available characters to use when generating the key. Defaults to alphanumeric.
Generates a key with the given length using the available characters.
ContentViewModel
CornerMan.ContentViewModel
ContentViewModel(template)
template
String (optional): Template for the ContentViewModel.
{ CornerManContentViewModel;}CornerMan; var fooVM = ;
getControlsForKey(key)
key
String=>
Array< ControlViewModel >: Returns the controls for the given key.
Gets the controls for the given key; any controls at the same key associated with children of this content ViewModel will be included. The controls are sorted by ascending using the order
of the control ViewModel.
getControlsObservableForKey(key)
key
String=>
Observable< Array< ControlViewModel > >: Returns an observable containing the ControlViewModels for the given key.
Gets the observable that contains controls for the given key; any controls at the same key associated with children of this ContentViewModel will be included. The controls are sorted by ascending using the order
of the ControlViewModel.
addControlAtKey(key, control)
key
Stringcontrol
ControlViewModel
Adds the given ControlViewModel to this ContentViewModel at the given key.
ControlViewModel
CornerMan.ControlViewModel
ControlViewModel(template, order)
template
String (optional): Template for the ControlViewModel.order
Number: Order of the ControlViewModel.
{ CornerManControlViewModel;}CornerMan; var fooControl = ;
getOrder()
=>
Number: Returns the order of this ControlViewModel.
getOrder(order)
order
Number
Set the order of this ControlViewModel.
Router API
Router
CornerMan.Router
Router(on404)
on404
Function (optional): Called when there is no registered route matching the current URL.
var router = { console;};
setOn404(on404)
on404
Function (optional): Called when there is no registered route matching the current URL.
Registers a function to be called when there is no registered route matching the current URL.
listen()
Starts the router listening for navigations. The current URL is immediately passed through the router.
registerRoute(route, callback [, callback ]...)
route
String: Route to register the callback(s) with.callbacks
Function: Functions called when the URL matches the given route.
The router
parameter can be a simple path, such as "/animals/dog"
or it can contains "slugs" that match variable URLs such as "/animals/:animal"
. This second example will match any URL in the form of "/animals/..."
but will not match "/animals/.../foo"
.
#registerRoute
accepts a variable number of callbacks following the route
parameter. The callbacks will be called in order with the following two parameters: request
(Object) and next
(Function). The request
parameter has two properties: 1. params
which contains the parameters parsed from the URL, and 2. query
which contains the querystring parsed into an object. The next
parameter is used to delegate to the next callback in the chain of registered callbacks. If a callback does not want of the following callbacks to be invoked, it simply shouldn't call next
.
var router = CornerMan; router; router; // User navigates to: /animals/dog?name=Max // The following lines will be printed:// params { animal: 'dog' }// query { name: 'Max' }// Handling URL.
get(route, callback [, callback ]...)
route
String: Route to register the callback(s) with.callbacks
Function: Functions called when the URL matches the given route.
Alias for #registerRoute.
navigate(url)
url
String
Navigates to the given URL.
hasHistory()
=>
Returns whether there is a history entry to go back to.
This is useful to know if the user navigated to the current URL through the router, or if the user landed directly on the current URL, say through a link from an external site.
back()
Navigates back to the previous URL.