Knockout is a great script library, but I find that many people do not understand it, so Scarlett has translated this book.
In this section, we will discuss what knockout can do and how to use it.
Knockout is a JavaScript script library
Knockout, in essence, is a JavaScript script library that can be used in the development of Web sites or Web pages. The ability to enhance scripting to provide a better customer experience. By default, it will not do anything unless you call knockout. This is an important point to distinguish between other script libraries or frameworks. You can download it from here.
Knockout is a MVVM (Model-view-viewmodel) Library
One important reason for creating knockout is that MVVM patterns can be applied in WEB development. MVVM is a kind of development method that is different from object-oriented, and can be tested by separating the logic of the interface from the business logic.
There are many reasons why we should write testable code (which can be used throughout the book), but the most important reason is the maintainability and quality assurance of the code. Write testable code, and the code becomes easier to maintain. Anyway, creating a Javascript program using the MVVM mode takes a little longer than writing a program with events that directly use the DOM.
The above diagram illustrates the basic components that are developed using MVVM and their communication relationships. You can see the View,model,viewmodel expressed as a block. The first thing to note is that Model,model is an object that usually directly represents the business logic in reality. It contains the properties and methods of the corresponding business logic. If you use Model to represent a car, you might have the following properties:
Maximum speed
Number of tires
Manufacturers
At the same time, there are some features:
Whistle
Forward
The second feature in MVVM is view, which consists of HTML tags, layouts, elements, colors, and other visual interface elements. There is no logic or code in it, and the definition is fully explained (all the instructions are done using pure HTML).
The third part of MVVC is that the Viewmodel,viewmodel provides a connection between View and Model. If you design a ViewModel to represent more than one car, its properties may be as follows:
Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/
Auto Set (Array)
Current car (Object)
Method
Add car
Sort of car
ViewModel allows you to implement the business logic in Model, while implementing the control logic of the interface in ViewModel. In terms of "decoupling," this is important for large WEB application development.
The last MVVM feature is called the Binding binding. Binding is an ideal way to connect user interface properties, events to ViewModel properties, and methods. An example of a binding can be a button on the interface, by clicking it to invoke the ViewModel addautomobile function, or by connecting multiple buttons on the interface to the Addautomobile function.
In MVVM, similar to view general use of the definition of syntax to explain, Binding also in the view by definition, Knockout is no exception, and a large number of HTML5-compatible data-bind properties for binding instructions.
A solid and graceful MVVM library, such as knockout, allows you to focus on the business logic and core functions of the application, rather than spending time on registering the event, canceling the registration, and updating the value of the input box when the data changes.
This is a great video, even if you don't understand English, you can learn how to use knockout.
Video Address: http://channel9.msdn.com/Events/MIX/MIX11/FRM08
Author: cnblogs Champion