- JavaScript:Moving to ES2015
- Ved Antani Simon Timms Narayan Prusty
- 553字
- 2021-07-09 19:07:38
JavaScript Model-View-* patterns
Model-View-Controller (MVC), Model-View-Presenter (MVP), and Model-View-ViewModel (MVVM) have been popular with server applications, but in recent years JavaScript applications are also using these patterns to structure and manage large projects. Many JavaScript frameworks have emerged that support MV* patterns. We will discuss a few examples using Backbone.js.
Model-View-Controller
MVC is a popular structural pattern where the idea is to pide an application into three parts so as to separate the internal representations of information from the presentation layer. MVC consists of components. The model is the application object, view is the presentation of the underlying model object, and controller handles the way in which the user interface behaves, depending on the user interactions.
Models
Models are constructs that represent data in the applications. They are agnostic of the user interface or routing logic. Changes to models are typically notified to the view layer by following the observer design pattern. Models may also contain code to validate, create, or delete data. The ability to automatically notify the views to react when the data is changed makes frameworks such as Backbone.js, Amber.js, and others very useful in building MV* applications. The following example shows you a typical Backbone model:
var EmployeeModel = Backbone.Model.extend({ url: '/employee/1', defaults: { id: 1, name: 'John Doe', occupation: null } initialize: function() { } }); var JohnDoe = new EmployeeModel();
This model structure may vary between different frameworks but they usually have certain commonalities in them. In most real-world applications, you would want your model to be persisted to an in-memory store or database.
Views
Views are the visual representations of your model. Usually, the state of the model is processed, filtered, or massaged before it is presented to the view layer. In JavaScript, views are responsible for rendering and manipulating DOM elements. Views observe models and get notified when there is a change in the model. When the user interacts with the view, certain attributes of the model are changed via the view layer (usually via controllers). In JavaScript frameworks such as Backbone, the views are created using template engines such as Handlebar.js (http://handlebarsjs.com/) or mustache.js (https://mustache.github.io/). These templates themselves are not views. They observe models and keep the view state updated based on these changes. Let's see an example of a view defined in Handlebar:
<li class="employee_photo"> <h2>{{title}}</h2> <img class="emp_headshot_small" src="{{src}}"/> <p class="employee_details"> {{employee_details}} </p> </li>
Views such as the preceding example contain markup tags containing template variables. These variables are delimited via a custom syntax. For example, template variables are delimited using {{ }}
in Handlebar.js. Frameworks typically transmit data in JSON format. How the view is populated from the model is handled transparently by the framework.
Controllers
Controllers act as a layer between models and views and are responsible for updating the model when the user changes the view attributes. Most JavaScript frameworks deviate from the classical definition of a controller. For example, Backbone does not have a concept called controller; they have something called a router that is responsible to handle routing logic. You can think of a combination of the view and router as a controller because a lot of the logic to synchronize models and views is done within the view itself. A typical Backbone router would look as follows:
var EmployeeRouter = Backbone.Router.extend({ routes: { "employee/:id": "route" }, route: function( id ) { ...view render logic... } });
- scikit-learn Cookbook
- Advanced Quantitative Finance with C++
- Java Web基礎與實例教程(第2版·微課版)
- Mastering Concurrency in Go
- Offer來了:Java面試核心知識點精講(原理篇)
- Selenium Design Patterns and Best Practices
- Android NDK Beginner’s Guide
- Python機器學習編程與實戰(zhàn)
- Bootstrap 4:Responsive Web Design
- C語言課程設計
- Visual C++開發(fā)入行真功夫
- 代替VBA!用Python輕松實現(xiàn)Excel編程
- R語言:邁向大數(shù)據(jù)之路(加強版)
- Application Development with Parse using iOS SDK
- 算法訓練營:海量圖解+競賽刷題(入門篇)