O'Reilly logo

Mastering KnockoutJS by Timothy Moran

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using service modules

Viewmodels should not contain all of your application code even in small applications. When possible, code should be broken out into non-viewmodel modules that encapsulate the work and can be reused. These modules are often called services.

For example, a viewmodel that gets data from the server doesn't need to know how that operation is handled, whether it uses jQuery's AJAX method, a websocket, or some other retrieval method. Putting this logic into a data service module not only makes it reusable by other viewmodels, it makes unit testing easier by limiting the scope of each object to its own work. The driving philosophy here is the single responsibility principle.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required