-
Notifications
You must be signed in to change notification settings - Fork 6
The MVVM pattern
While there is no requirement to use any design pattern to make a "OneJS" compatible view other than to implement the IView interface, OneJS makes it simple for you to define your own views (think of a view as a conceptual 'control') in a declarative templated way if you choose, so that you can define reusable content easily.
All template based controls adhere to an MVVM pattern. The pattern separates concepts cleanly which leads to better maintainability:
M odels are an abstract concept that encapsulate a domain model or a data access layer. For example, you might create a class called ItemDataSource that provides CRUD methods for fetching/manipulating items.
V iews abstract the view logic (rendering, updating, click handling). This might be code that interacts with the DOM directly. This code may also be generated automatically by a template compiler to simplify and standardize common. For example, you might create an ItemTile view that can render an item.
V iew M odels expose the data contract that the view consumes. Often, but not always, they glue the View and Models together. ViewModels can provide default values and abstract logic to fetch the data required and 'fit' into that contract.
First, View code needs to be separated from the data logic. As the ui designs are tweaked, having JUST the ui elements and logic for manipulating the ui all in one place, separated from any ui state <> service state glue logic, makes it smaller and easier to manage and tweak.
View models also are specific to how the data comes in. If you write a "LeftNav" control that you want to use in website A and website B, you may define a common data contract. Your "LeftNavModel" view model might require a list of links. In site A, you may subclass that data contract as "SiteALeftNavModel", interact with site A specific data, and pipe it into the common LeftNav contract requirements. You also do the same exercise for site B. If the view and view model code were all one big blob, it would be more difficult to abstract this.