摘要: 本文介绍了模型-视图-控制器模式在 JavaScript 中的实现, 在 JavaScript 中,程序员可以根据自己的口味选择编程风格:面向过程或面向对象。
原文:Model-View-Controller (MVC) with JavaScript
作者:Alex@Net
译文:JavaScript 的 MVC 模式
译者:justjavac
本文介绍了模型-视图-控制器模式在 JavaScript 中的实现。
我喜欢 JavaScript,因为它是在世界上最灵活的语言之一。 在 JavaScript 中,程序员可以根据自己的口味选择编程风格:面向过程或面向对象。 如果你是一个重口味,JavaScript 一样可以应付自如: 面向过程,面向对象,面向方面, 使用 JavaScript 开发人员甚至可以使用函数式编程技术。
这篇文章中,我的目标是编写一个简单的 JavaScript 组件,来向大家展示一下 JavaScript 的强大。 该组件是一个可编辑的项目列表(HTML中的 select 标签):用户可以选择某一项并删除它,或者添加新的项目到列表中。 组件将由三个类构成,分别对应着 MVC 设计模式的模型-视图-控制器。
这篇文章只是一个简单的指导。 如果你希望在实际的项目中使用它,你需要进行适当的修改。 我相信你拥有创建和运行 JavaScript 程序的一切:大脑,双手,文本编辑器(如记事本),浏览器(例如我的最爱 Chrome)。
既然我们的代码要使用 MVC 模式,因此我在这里简单介绍一个这个设计模式。 MVC 模式的英文名称是 Model-View-Controller pattern,顾名思义,其主要部分组成:
- 模型Model(),用于存储程序中使用到的数据;
- 视图(View),用不同的表现形式来呈现数据;
- 控制器(Controller),更新模型。
在维基百科对 MVC 体系结构的定义中,它由如下三部分组成:
模型(Model) -“数据模型”(Model)用于封装与应用程序的业务逻辑相关的数据以及对数据的处理方法。 “模型”有对数据直接访问的权力。 “模型”不依赖“视图”和“控制器”,也就是说,模型不关心它会被如何显示或是如何被操作。
视图(View) - 视图层能够实现数据有目的的显示,通常是一个用户界面元素。 在视图中一般没有程序上的逻辑。 在 Web 应用程序中的 MVC,通常把显示动态数据的 html 页面称为视图。
控制器(Controller) - 处理和响应事件,通常是用户操作,并监控模型上的变化,然后去修改视图。
The data of the component is a list of items, in which one particular item can be selected and deleted. So, the model of the component is very simple - it is stored in an array property and selected item property; and here it is:
我们将基于 MVC 实现一个数据列表组件,列表中的项目可以被选择和删除。 因此,组件模型是非常简单的 - 它只需要两个属性:
- 数组 _items 用来存储所有元素
- 普通变量 _selectedIndex 用来存储选定的元素索引
代码如下:
06 | function ListModel(items) { |
08 | this ._selectedIndex = -1; |
10 | this .itemAdded = new Event( this ); |
11 | this .itemRemoved = new Event( this ); |
12 | this .selectedIndexChanged = new Event( this ); |
15 | ListModel.prototype = { |
16 | getItems : function () { |
17 | return [].concat( this ._items); |
20 | addItem : function (item) { |
21 | this ._items.push(item); |
22 | this .itemAdded.notify({item : item}); |
25 | removeItemAt : function (index) { |
28 | item = this ._items[index]; |
29 | this ._items.splice(index, 1); |
30 | this .itemRemoved.notify({item : item}); |
32 | if (index === this ._selectedIndex) { |
33 | this .setSelectedIndex(-1); |
37 | getSelectedIndex : function () { |
38 | return this ._selectedIndex; |
41 | setSelectedIndex : function (index) { |
44 | previousIndex = this ._selectedIndex; |
45 | this ._selectedIndex = index; |
46 | this .selectedIndexChanged.notify({previous : previousIndex}); |
Event 是一个简单的实现了观察者模式(Observer pattern)的类:
01 | function Event(sender) { |
02 | this ._sender = sender; |
07 | attach : function (listener) { |
08 | this ._listeners.push(listener); |
11 | notify : function (args) { |
14 | for (index = 0; index < this ._listeners.length; index += 1) { |
15 | this ._listeners[index]( this ._sender, args); |
View 类需要定义控制器类,以便与它交互。 虽然这个任务可以有许多不同的接口(interface),但我更喜欢最简单的。 我希望我的项目是在一个 ListBox 控件和它下面的两个按钮:“加号”按钮添加项目,“减”删除所选项目。 组件所提供的“选择”功能则需要 select 控件的原生功能的支持。
一个 View 类被绑定在一个 Controller 类上, 其中「…控制器处理用户输入事件,通常是通过一个已注册的回调函数」(wikipedia.org)。
下面是 View 和 Controller 类:
007 | function ListView(model, elements) { |
009 | this ._elements = elements; |
011 | this .listModified = new Event( this ); |
012 | this .addButtonClicked = new Event( this ); |
013 | this .delButtonClicked = new Event( this ); |
018 | this ._model.itemAdded.attach( function () { |
022 | this ._model.itemRemoved.attach( function () { |
027 | this ._elements.list.change( function (e) { |
028 | _this.listModified.notify({ index : e.target.selectedIndex }); |
031 | this ._elements.addButton.click( function () { |
032 | _this.addButtonClicked.notify(); |
035 | this ._elements.delButton.click( function () { |
036 | _this.delButtonClicked.notify(); |
040 | ListView.prototype = { |
045 | rebuildList : function () { |
046 | var list, items, key; |
048 | list = this ._elements.list; |
051 | items = this ._model.getItems(); |
053 | if (items.hasOwnProperty(key)) { |
054 | list.append($( ' + items[key] + '' )); |
058 | this ._model.setSelectedIndex(-1); |
067 | function ListController(model, view) { |
073 | this ._view.listModified.attach( function (sender, args) { |
074 | _this.updateSelected(args.index); |
077 | this ._view.addButtonClicked.attach( function () { |
081 | this ._view.delButtonClicked.attach( function () { |
086 | ListController.prototype = { |
087 | addItem : function () { |
088 | var item = window.prompt( 'Add item:' , '' ); |
090 | this ._model.addItem(item); |
094 | delItem : function () { |
097 | index = this ._model.getSelectedIndex(); |
099 | this ._model.removeItemAt( this ._model.getSelectedIndex()); |
103 | updateSelected : function (index) { |
104 | this ._model.setSelectedIndex(index); |
当然,Model, View, Controller 类应当被实例化。
下面是一个使用此 MVC 的完整代码:
02 | var model = new ListModel([ 'PHP' , 'JavaScript' ]), |
04 | view = new ListView(model, { |
06 | 'addButton' : $( '#plusBtn' ), |
07 | 'delButton' : $( '#minusBtn' ) |
10 | controller = new ListController(model, view); |
14 | <select id= "list" size= "10" style= "width: 15em" ></select><br/> |
15 | <button id= "plusBtn" > + </button> |
16 | <button id= "minusBtn" > - </button> |