Book Image

Mastering KnockoutJS

By : Timothy Moran
Book Image

Mastering KnockoutJS

By: Timothy Moran

Overview of this book

Table of Contents (16 chapters)
Mastering KnockoutJS
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Sticking to MVVM


Knockout was designed with the Model-View-ViewModel (MVVM) pattern in mind. While it is possible to develop applications using Knockout and other design patterns, sticking to MVVM will produce a natural alignment between Knockout and your own code.

The view and the viewmodel

The separation of concerns is the key here. Don't introduce view concepts such as DOM elements or CSS classes into your viewmodel; these belong in the HTML. Limit or avoid business logic and inline binding functions in your view; these belong as properties or functions in your viewmodel. Keeping these two separated makes it possible for the work to be divided and parallelized, allows the viewmodel to be reusable, and makes it possible to unit test the viewmodel.

Cluttering the viewmodel

Animation handlers are a good example of view logic that often ends up in the viewmodel. The foreach binding handler has several postprocessing hooks (such as afteradd, afterrender, and beforeremove) that are intended to...