So, what is knockout? A javascript library. At its core, a javascript library that can be included in a web site/application too add js functionality and enhance user’s experience. Knockout – A MVVM (model-View-ViewModel) library.
A Model is an object that usually most directly represent a real-world object in the business system. It contains properties and functions that have business like names and reactions.
Such as an Automobile:
properties: MaxSpeed(Number), TireSize(Number), Manufacturer(String)
functions: Honk() and DriveForward()
A View is the HTML markup that describe the layout, elements (buttons, textboxes), colors and other visual pieces. No logic or code embedded in it. It’s completely declarative.
A ViewModel a connection between the View and the Model. (???) If you are making a ViewModel for a View that was designed to display automobiles, it might have properties such as: AutomobileCollection(Array), SelectedAutomobile(Obj), and functions: AddAutomobile(), SortAutomobile() etc.
Binding: connecting the properties and events of user interface elements (HTML) to functions and properties of an object such as ViewModel. Example: Connect a [AddAutomobile] button (and/or other buttons) with ViewModel’s AddAutomobile() method.
Installation
Step 1: make sure you have a text editor (we use VS 2012), web server, web browser
Step 2: Create a starter site. Download from http://html5boilerplate.com/ a starter site that will be used for this exercise. Open it in VS 2012. ![]()
Also, update the index.html to:![]()
Step 3: Download knockout!
Use Nuget Manager to download and install KnockoutJS![]()
The knockout files will be copied under the /Scripts folder. Copy them to under /js so all JavaScript files are under the same roof.![]()
Step 4: Create main application javascript file – App.js – and place in /js.
Add the reference to the 2 javascript files in the starter HTML file![]()
(Include javascript file at the end of the HTML file so the visual components can be displayed first)
Step 5: Run the app. ![]()
Verify in Filddler2 that the browser did download the two js files.![]()
Now build an Inventory Management application
(1) Defining a namespace in App.js – just add the following two lines
// defining a name space “myApp”.
window.myApp={};
(2) Creating Model (class) under the namespace
Define a [Product] class under the namespace [myApp]![]()
// Usage
// create an instance of the Product class
var productA = new myApp.Product();
// “set” the ‘sku’ property
productA.sku(‘12345’)
// “get” the ‘sku’ property value
var skuNumber = productA.sku();
(3) Creating a view for the Model – now that we have a product, we want to let user visually see the product on the screen.
Add these HTML t index.html’s “content” div.![]()
(4) Creating a ViewModel to manage out Models (/JS/ProductsViewModel.js)
Similar to creating the Product class. ViewModel’s contains functions/methods that manipulate models (class), for example, AddProduct( ), RemoveProduct( ) etc.![]()
(5) In JS, the data structure used for maintaining list of objects is array. Knockout uses “Observable Array”. This types of array raises event when it changes. The event allows Knockout to keep our UI up-to-date whenever Observable Array changes.
Add a new property to our new ViewModel –
|
(6) Add/removing models from an Observable Array (still, update the ProductsViewModel.js)
addNewProduct : first instantiate a new Product, then make it the currently selected product.
doneEditingProduct is to commit the save action.![]()
Logic for removing: checking for null, and then pull out of the collection
Add 3 buttons to the index.html ad bind the click event to the functions defined in the ProductsViewModel.js![]()
Step 7: Editing Model properties. The current index.html s display only, but we need a way to enter products info so we can add/update them. So update the index.html so it’s a form with input elements.
Step 8 – Setting up master-details view
First we need to show/hide the 3 buttons based on whether there is any product selected. So set the visible property based on the selectedProduct() function.![]()
hide/Show button depending on whether there is selected product.![]()
Now we need to provide our users a way to view the master list of products that they’re managing. Often we see grids, tables, unordered lists, or other widgets designed to display list of objects, but knockout is powerful enough that we can use simple HTML to provide a view into productCollection.
We’re going to provide a basic list-view using a select element. Knockout provides a [options] binding that allows us to bind an observable array to a select element.
11:41pm, 01/15/2013, Page 20
Add these two to the ProductsViewModel – don’t know why, but will see later.![]()
In HTML, add this <select>….</select>, using “options” to bind to the productCollection.![]()
optionsText – which property of each element in the Observable Array we want to appear in the select element’s options? It’s like a bind field.
What if you want to bind to sku + ‘ ‘ + description? (bind to two fields), then you need to add a [Computed Observables] to the productsViewModel, like this:![]()
Then you can use the new property in the optionsText: ![]()
Step 9: Applying Binding – for this app to run, need to kick off the binding process. Need to wait (1) the scripts are loaded and (2) viewmodels initialized before the process is invoked. Add this to App.js
(Application still not working – got error (sku id undefined) – will check again after finishing this book)![]()
Page 23: Top features you need to know about
One of the best part about Knockout is extensibility.
Subscribables
Under the cover of observable, observable arrays and computed observables is subscribable. It’s simply an object that has three methods and an array of subscription.
methods:
(1) subscribe – adds a Subscription callback for a certain topic to be invoked when subscriptions are “notified”. The default topic (event???) is “changed”.
(2) notifySubscribers: This calls all subscriptions for a certain topic and passes on argument to all the subscription;s callbacks.
(3) extend – applies an extender to the subscribable object.
Observable
one of the first objects that leverages the Subscribable functionality. Most simple, powerful of the knockout family.
Skipping the rest of them book. Sample codes are hard to understand. Move on to another one and maybe come back later to review.
01/16/2013, 09:14pm