-
Notifications
You must be signed in to change notification settings - Fork 1
/
MCVRExample.html
245 lines (222 loc) · 8.87 KB
/
MCVRExample.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=7" />
<title>MCRV 设计模式 Demo</title>
<style type="text/css">
/********表现*********/
table{width:100%;border-collapse: collapse;}
td{border: 1px solid black;padding: 2px;}
#container {width:800px;margin:0px auto;}
#tbUsers{margin:20px auto;}
#tbUsers th{background-color: navy;color:white;text-align: center;vertical-align: middle;border:1px solid navy}
#tbUsers td{text-align: center;}
.editCaption{width:100px;text-align: right;}
.buttonMargin{margin:0px 20px;}
.buttonContainer{text-align: center;vertical-align: middle;height:50px}
</style>
<script src="./jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<!-------结构--------->
<div id="container">
<table id="tbUsers">
<thead><th>id</th><th>姓名</th><th>年龄</th><th>修改</th></thead>
<tbody/>
</table>
<div style="display: none;" id="dvEditPanel">
<form id="frmModify" name="frmModify">
<table>
<tr>
<td class="editCaption"> id: </td>
<td><span id="spID"></span></td>
</tr>
<tr>
<td class="editCaption"> 姓名: </td>
<td><input type="text" size="20" id="txtName"/></td>
</tr>
<tr>
<td class="editCaption"> 年龄: </td>
<td><input type="text" size="20" id="txtAge"/></td>
</tr>
<tr>
<td colspan="2" class="buttonContainer">
<button id="btnSubmitModify" class="buttonMargin" type="button">提交</button>
<button id="btnCancelModify" class="buttonMargin" type="button">取消</button>
</td>
</tr>
</table>
</form>
</div>
</div>
<script>
/***************************行为********************************/
var UserManagerMCR;
$(function()
{
UserManagerMCR=new MCR(UserController,UserModel,UserRenderer);
});
/*
* MCR 三元组
*/
function MCR(Controller,Model,Renderer)
{
this.controller=new Controller();
this.model=new Model();
this.renderer=new Renderer();
this.controller.model=this.model;
this.controller.renderer=this.renderer;
this.model.controller=this.controller;
this.renderer.controller=this.controller;
if(typeof this.model.init=="function")
{
this.model.init();
}
if(typeof this.renderer.init=="function")
{
this.renderer.init();
}
if(typeof this.controller.init=="function")
{
this.controller.init();
}
}
/*
* 控制器
*/
function UserController()
{
this.init=function()
{
this.initUserList();
}
this.initUserList=function()
{
var list=this.model.getUserList();
this.renderer.renderUserList(list);
}
this.beginModify=function(data)
{
var user=this.model.getUserByID(data.id);
this.renderer.showModifyUI(user);
}
//提交修改
this.submitModify=function(user)
{
var result=this.model.modifyUser(user);
if(result.success)
{
var list=this.model.getUserList();
this.renderer.renderUIWhenSubmitModifySuccess(list);
}
else
{
alert(result.msg);
}
}
//取消修改
this.cancelModify=function()
{
this.renderer.hideModifyUI();
}
}
/*
* 模型
*/
function UserModel()
{
//模拟的数据,实际应用中经常从服务器获取
this.init=function()
{
this.data = [
{id:0,name:"John",age:22},
{id:1,name:"Tom",age:30},
{id:2,name:"Tony",age:25}
];
}
//获得用户数据列表
this.getUserList=function()
{
//todo ,可能ajax从后端返回
return this.data;
}
//获得用户数据
this.getUserByID=function(id)
{
var ix;
$.each(this.data,function(i,item){if(item["id"]==id ) { ix=i; return false;}});
return this.data[ix];
}
//修改用户数据
this.modifyUser=function(user)
{
var result={success:true,msg:"修改成功"};
//todo,验证参数user
//todo,修改用户数据
$.each(this.data,function(i,item)
{
if(item["id"]==user["id"])
{
item["name"]=user["name"];
item["age"]=user["age"]
return false;
}
});
return result;
}
}
/*
* 呈现器
*/
function UserRenderer()
{
this.init=function()
{
var me=this;
$("#btnSubmitModify").click(function()
{
var user={id:$("#spID").text(),name:$("#txtName").val(),age:$("#txtAge").val()};
me.controller.submitModify(user);
});
$("#btnCancelModify").click(function()
{
me.controller.cancelModify();
});
$("#tbUsers .modify").live("click",function()
{
var id=$(this).attr("uid");
me.controller.beginModify({"id":id});
});
}
this.renderUserList=function(list)
{
var htm=[];
for(var ix=0;ix<list.length;ix++)
{
htm.push("<tr><td>" +list[ix]["id"]+"</td>" +"<td>"+list[ix]["name"]+"<td>"+list[ix]["age"]+"</td>"
+"<td>"+"<a class='modify' href='javascript:void(0)' uid='"+list[ix]["id"]+"'>修改</a></td>"+"</tr>");
}
$("#tbUsers").children("tbody").html(htm.join(""));
}
this.showModifyUI=function(user)
{
$("#dvEditPanel").show();
$("#spID").text(user["id"]);
$("#txtName").val(user["name"]);
$("#txtAge").val(user["age"]);
}
this.hideModifyUI=function()
{
document.frmModify.reset();
$("#dvEditPanel").hide();
}
this.renderUIWhenSubmitModifySuccess=function(list)
{
this.hideModifyUI();
this.renderUserList(list);
}
}
</script>
</body>
</html>