官术网_书友最值得收藏!

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...
  }
});
主站蜘蛛池模板: 吉木萨尔县| 大悟县| 涞源县| 正镶白旗| 城步| 社会| 永泰县| 永胜县| 轮台县| 尉犁县| 玉林市| 库尔勒市| 多伦县| 合阳县| 曲阜市| 无棣县| 兴安盟| 长沙市| 呼伦贝尔市| 柳河县| 鲁山县| 巫山县| 行唐县| 苍溪县| 温泉县| 苗栗市| 安塞县| 讷河市| 舞阳县| 荆门市| 岑巩县| 资源县| 长宁县| 新源县| 泸溪县| 温州市| 昌江| 黎平县| 长垣县| 九龙城区| 嘉善县|