Documente Academic
Documente Profesional
Documente Cultură
Agenda
Introduction
What is knockout?
Observables
Bindings
Custom Bindings
Utilities
Data Features
JavaScript
Data Bindings
Binds data from a source(ViewModel) to a target(Element on the view).
Supports one-way and two-way bindings.
Observables
Objects that hold data and can be observed for any changes.
Observables notify subscribes of changes.
Built on the GoF design pattern: Observable.
Dependency Tracking
Elements using data from an Observables do not have to explicitly subscribe
for changes.
Knockout implements dependency tracking, a mechanism by which all
dependent elements are automatically updated of changes.
Bindings
text
Causes the associated DOM element to display the text value of your
parameter.
Ex: <p data-bind="text: message>
Cannot pass HTML content
value
Links the associated DOM elements value with a property on your
view model.
Ex: <input data-bind="value: userId" />
Additional parameters: valueUpdate
values: keyup, keypress, afterkeydown.
Observable
JavaScript functions
Not all browsers support JavaScript getters/setters
Internally KOs bindings observe the observables
Three types :
Observable
Used for view model properties
ObservableArrays
Used for Collections
Computed
Encapsulates one or more observables
Bindings
checked
links a checkable form control.
<input type="checkbox" data-bind="checked: hasRecords" />
visible
Causes the associated DOM element to become hidden or visible
Ex: <div data-bind="visible: hasData">
css
Adds or removes one or more named CSS classes to the associated DOM element.
<div data-bind="css: { sampleClass: balance() < 10000 }">
style
adds or removes one or more style values to the associated DOM element.
<div data-bind="style: { color: balance() < 10000 ? 'red' : 'black' }">
Bindings
click
Adds an event handler to a DOM element for the onclick event.
Ex:
<button data-bind="click: saveData">Click me</button>
The handler receives the current model value as the first argument,
and the DOM event as the second argument.
Return true to proceed to the default click action.
Use clickBubble: false to prevent bubbling of the event.
submit
Adds an event handler to a DOM element for the submit action.
return true for the proceed to the default submit action.
Observable Arrays
options
controls what options should appear in a drop-down listor multi-
select list.
Ex: <select data-bind="options: countries"></select>
Can be bound to an array of objects.
Additional Parametrs
optionsCaption
optionsText
optionsValue
optionsIncludeDestroyed
optionsAfterRender
Bindings
foreach
Binding that iterates an array and binds markup to the corresponding
array item.
Used for rendering lists and arrays
Ex: <tbody data-bind="foreach: employees">
$data: refers to each item in the array
$index: zero-based index of the current item.
$parent: handle to the parent of the bound array.
Knockout: Custom Bindings
ko.toJS
Clones the view models object graph.
Substitutes each observable with the current value of that observable
Result will be a plain copy that contains only your data and no
Knockout-related artifacts
ko.toJSON
This produces a JSON string representing your view models data.
Internally invokes ko.toJSon the view model
Uses the browsers native JSON serializer on the result.
For older browsers (e.g., IE 7 or earlier), you must also reference the
json2.jslibrary.
Knockout: Extending Observables
jQuery in Action
Manning Publication
Ajax
OReilly