-
Notifications
You must be signed in to change notification settings - Fork 1
/
MCRFrameworkExample.html
353 lines (319 loc) · 16.5 KB
/
MCRFrameworkExample.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
<!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}
/*.qz_dialog_layer_main {
background-color: #FFFFFF;
border: 1px solid #6B97C1;
position: relative;
}
.qz_dialog_layer_title
{
background: none repeat-x scroll 0 0 #8BACCF;
height: 28px;
}
.qz_dialog_layer_title h3
{
color: #FFFFFF;
float: left;
font-size: 13px;
height: 28px;
line-height: 28px;
margin: 0;
overflow: hidden;
text-indent: 8px;
}
.qz_dialog_layer_title .qz_dialog_btn_close {
background: url("../img/pupop_bg.png") no-repeat scroll 0 -30px transparent;
border: 0 none;
cursor: pointer;
float: right;
font-size: 0;
height: 19px;
line-height: 100px;
margin-right: 5px;
width: 34px;
}
.qz_dialog_layer_cont {
background-color: #FFFFFF;
color: #5F5F5F;
} */
</style>
<link rel="stylesheet" type="text/css" media="screen" href="./cupertino/jquery-ui-1.7.3.custom.css" />
<script src="./jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="./jquery-ui-1.7.2.custom.min.js" type="text/javascript" charset="utf-8"></script>
<script src="./MCR-Core.js" type="text/javascript" charset="utf-8"></script>
<script src="./MCR-Debug.js" type="text/javascript" charset="utf-8"></script>
<!--<script type="text/javascript" src="https://getfirebug.com/firebug-lite-beta.js"></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 style='z-index: 9001; position: absolute;width: 400px;top:300;left:300px;'>
<div id='___dvMCRContainer' style='background-color: #FFFFFF;border: 1px solid #6B97C1;position: relative;'>
<div id='dialog_head_1' style='background: none repeat-x scroll 0 0 #8BACCF;height: 28px;'>
<h3 id='dialog_title_1' style='color: #FFFFFF;float: left;font-size: 13px;height: 28px;line-height: 28px;margin: 0;overflow: hidden;text-indent: 8px;'>登录Qzone</h3>
<span style='color:white;font-size: 12px;cursor: pointer;float: right;height: 19px;margin:0px 5px 0px 0px;'>╳</span>
</div>
<div style='height:200px;'>
</div>
<div style='text-align:center;padding:20px 0px;'>
<button type='button' style='margin:0px 15px;'>提交</button><button type='button' style='margin:0px 15px;'>取消</button><button type='button' style='margin:0px 15px;'>重置</button>
</div>
</div>
</div>-->
<!-- <div class="qz_dialog_layer" style="z-index: 9001; position: absolute; top: 496px; left: 529px; width: 402px;" id="qz_dialog_instance_qzDialog1">
<div class="qz_dialog_layer_main" id="dialog_main_1">
<div class="qz_dialog_layer_title" id="dialog_head_1" style="cursor: move;">
<h3 id="dialog_title_1">登录Qzone</h3><button class="qz_dialog_btn_close" title="关闭" id="dialog_button_1">
<span class="none">╳</span></button>
</div>
<div style="height: 204px;" class="qz_dialog_layer_cont" id="dialog_content_1">
<div style="border-top:1px solid #CCDAE2;margin:0 21px;padding:15px 2px;">
<a href="http://qzone.qq.com/index.html" target="_blank" onclick="this.blur();" onmouseout="this.style.textDecoration = 'none';this.style.color='#097FD1';" onmouseover="this.style.textDecoration = 'underline';this.style.color = '#000';" style="border-right:1px solid #D0D3D7;display:inline-block;height:18px;line-height:18px;text-decoration:none;vertical-align:baseline;color:#097FD1;padding:0 9px 0 29px;color:#097FD1;">空间社区</a>
<a href="http://dynamic.qzone.qq.com/cgi-bin/portal/cgi_select_activity" target="_blank" onclick="this.blur();" onmouseout="this.style.textDecoration = 'none';this.style.color='#097FD1';" onmouseover="this.style.textDecoration = 'underline';this.style.color = '#000';" style="border-right: 1px solid rgb(208, 211, 215); display: inline-block; height: 18px; line-height: 18px; text-decoration: none; vertical-align: baseline; padding: 0pt 9px; color: rgb(9, 127, 209);">注册空间</a>
<a href="http://20050606.qzone.qq.com/" target="_blank" onclick="this.blur();" onmouseout="this.style.textDecoration = 'none';this.style.color='#097FD1';" onmouseover="this.style.textDecoration = 'underline';this.style.color = '#000';" style="border-right: 1px solid rgb(208, 211, 215); display: inline-block; height: 18px; line-height: 18px; text-decoration: none; vertical-align: baseline; padding: 0pt 9px; color: rgb(9, 127, 209);">官方Qzone</a>
<a href="http://im.qq.com/" target="_blank" onclick="this.blur();" onmouseout="this.style.textDecoration = 'none';this.style.color='#097FD1';" onmouseover="this.style.textDecoration = 'underline';this.style.color = '#000';" style="display: inline-block; height: 18px; line-height: 18px; text-decoration: none; vertical-align: baseline; padding: 0pt 9px; color: rgb(9, 127, 209);">下载最新版QQ</a>
</div>
</div>
</div>
</div> -->
</div>
<script>
/***************************行为********************************/
var UserManagerMCR;
$(function()
{
UserManagerMCR=MCR(UserModel,UserController,UserRenderer);
/*var div=document.createElement("DIV");
div.style.zIndex="10001";
div.style.width="400px";
div.style.position="absolute";
div.id="___dvMCRContainer";
div.style.borderWidth="1px";
div.style.borderStyle="solid";
div.style.borderColor="#6B97C1";
div.innerHTML="<div id='dialog_head_1' style='background: none repeat-x scroll 0 0 #8BACCF;height: 28px;'> "
+" <h3 id='dialog_title_1' style='color: #FFFFFF;float: left;font-size: 13px;height: 28px;line-height: 28px;margin: 0;overflow: hidden;text-indent: 8px;'>登录Qzone</h3> "
+ " <span style='color:white;font-size: 18px;cursor: pointer;float: right;height: 19px;margin:0px 5px 0px 0px;'>x</span> "
+ "</div> "
+" <div style='height:200px;'> 创建的DIV"
+" </div>";
div.style.display="none";
document.body.appendChild(div);
var docuSize=$.MCR.getDocumentSize();
var divWidth=MCR.getWidth(div);
div.style.top=(docuSize.height/2)+"px";
div.style.left=(docuSize.width/2-divWidth/2)+"px";
div.style.display="block";*/
});
/*
* 控制器
*/
function UserController()
{
this.init=function()
{
this.initUserList();
}
this.initUserList=function(data)
{
var list;
if(!$.isArray(data))
list=this.model.getUserList();
else
list=data;
if(list)
this.renderer.renderUserList(list);
}
this.beginModify=function(id)
{
var user=this.model.getUserByID(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()
{
/*var data=[
{"id":0,"name":"John","age":22},
{"id":1,"name":"Tom","age":30},
{"id":2,"name":"Tony","age":25}
];
this.cache.set("UserList", data);*/
var me=this;
$.ajax({
type:"GET",
url:'./jsonData.html',
dataType:"json"
,async:true
,success:function(json)
{
me.cache.set("UserList", json);
me.controller.initUserList();
//me.asyncCall("initUserList",json);
},
error:function()
{
alert("从server获取用户列表时发生错误!");
}
,cahce:false
});
}
//获得用户数据列表
this.getUserList=function()
{
//todo ,可能ajax从后端返回
return this.cache.get("UserList");
}
//获得用户数据
this.getUserByID=function(id)
{
var ix;
var data=this.getUserList();
$.each(data,function(i,item)
{
if(item["id"]==id ) { ix=id; return false;}
});
var ret=data[ix];
return ret;
}
//修改用户数据
this.modifyUser=function(user)
{
var result={success:true,msg:"修改成功"};
//todo,验证参数user
//todo,修改用户数据
var data=this.getUserList();
$.each(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);
});
}
this.renderUserList=function(list)
{
if(!(list instanceof Array)) return;
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>