root 8 years ago
parent
commit
bd334074e9
3 changed files with 75 additions and 44 deletions
  1. 5 9
      js/form.js
  2. 1 5
      js/spa.js
  3. 69 30
      js/util.js

+ 5 - 9
js/form.js

@ -16,7 +16,7 @@ $.define(["jQuery", "util"], "form", function($, util) {
16 16
					v = vd;
17 17
				},
18 18
				reset: function() {
19
					dv = data;
19
					v = dv;
20 20
				},
21 21
				validate: function() {
22 22
					return util.validate(rules, this);
@ -35,6 +35,7 @@ $.define(["jQuery", "util"], "form", function($, util) {
35 35
			}
36 36
			return true;
37 37
		},
38
		serialize=util.serialize,
38 39
		/* create form instance by jQuery obj */
39 40
		bf = function($e) {
40 41
			if($e.length === 1) {
@ -123,22 +124,17 @@ $.define(["jQuery", "util"], "form", function($, util) {
123 124
					},
124 125
					doGet: function(url, sh, eh, config) {
125 126
						if(this.validate()) {
126
							util.get(url, this.val(), sh, eh, config);
127
							util.get(url, serialize(this.val()), sh, eh, config);
127 128
						}
128 129
					},
129 130
					doPost: function(url, sh, eh, config) {
130 131
						if(this.validate()) {
131
							util.post(url, this.val(), sh, eh, config);
132
							util.post(url, serialize(this.val()), sh, eh, config);
132 133
						}
133 134
					},
134 135
					doPut: function(url, sh, eh, config) {
135 136
						if(this.validate()) {
136
							util.put(url, this.val(), sh, eh, config);
137
						}
138
					},
139
					doDel: function(url, sh, eh, config) {
140
						if(this.validate()) {
141
							util.del(url, this.val(), sh, eh, config);
137
							util.put(url, serialize(this.val()), sh, eh, config);
142 138
						}
143 139
					}
144 140
				};

+ 1 - 5
js/spa.js

@ -8,7 +8,7 @@ $.define(["jQuery", "util", "doc", "win", "body"], "spa", function($, util, doc,
8 8
		cssCache = {},
9 9
		htmlCache = { "#": "#" },
10 10
		modelCache = {},
11
		scriptCache {}, resUri, menuUri, menuEle,
11
		scriptCache = {}, resUri, menuUri, menuEle,
12 12
		main, mainEle,
13 13
		errHand = {
14 14
			"defErrHand": function(err) {
@ -338,11 +338,7 @@ $.define(["jQuery", "util", "doc", "win", "body"], "spa", function($, util, doc,
338 338
				id = id.substring(1);
339 339
			} else return;
340 340
			if(main && id == main.id) return;
341
			if(main) {
342
				cleanMain();
343
			}
344 341
			var model = modelCache[id];
345

346 342
			if(model) {
347 343
				showMainInternal(model);
348 344
			} else {

+ 69 - 30
js/util.js

@ -18,11 +18,11 @@ $.define(["jQuery", "body", "win","doc"], "util", function($, $body, win,doc) {
18 18
			this.prev.css("display", "block");
19 19
			layer_curr = this.prev;
20 20
		},
21
		fh = function(e) {
21
		hideMsg = function(e) {
22 22
			$(this).parent().hide();
23 23
		},
24
		bh = function($p) {
25
			$p.find(".click-hide-parent").on("click", fh);
24
		bindHide = function($p) {
25
			$p.find(".click-hide-parent").on("click", hideMsg);
26 26
		},
27 27
		fillContent = function($c /* child node [String|Function|jQObj] */ , $p) {
28 28
			if($c) {
@ -59,33 +59,33 @@ $.define(["jQuery", "body", "win","doc"], "util", function($, $body, win,doc) {
59 59
		},
60 60
61 61
		// function error message
62
		fem = function($c, $t) {
62
		errMsg = function($c, $t) {
63 63
			var $e = $(
64 64
					"<div class='err-ctn'><i class='icon click-hide-parent'></i></div>")
65 65
				.appendTo(errDiv);
66 66
			$("<span></span>").appendTo($e).html($c);
67
			bh($e);
67
			bindHide($e);
68 68
			setTimeout(function() {
69 69
				$e.remove()
70 70
			}, $t ? $t : 10000);
71 71
		},
72 72
		// function warn message
73
		fwm = function($c, $t) {
73
		warnMsg = function($c, $t) {
74 74
			var $w = $(
75 75
					"<div class='warn-ctn'><i class='icon click-hide-parent'></i></div>")
76 76
				.appendTo(warnDiv);
77 77
			$("<span></span>").appendTo($w).html($c);
78
			bh($w);
78
			bindHide($w);
79 79
			setTimeout(function() {
80 80
				$w.remove()
81 81
			}, $t ? $t : 3000);
82 82
		},
83
		fm = function($c, $t) {
83
		msg = function($c, $t) {
84 84
			var $m = $(
85 85
					"<div class='warn-ctn'><i class='icon click-hide-parent'></i></div>")
86 86
				.appendTo(warnDiv);
87 87
			$("<span></span>").appendTo($w).html($c);
88
			bh($m);
88
			bindHide($m);
89 89
			setTimeout(function() {
90 90
				$w.remove()
91 91
			}, $t ? $t : 3000);
@ -104,7 +104,7 @@ $.define(["jQuery", "body", "win","doc"], "util", function($, $body, win,doc) {
104 104
				loadingDiv.hide()
105 105
			}
106 106
		},
107
		am = function(title, content, hand) {
107
		alertMsg = function(title, content, hand) {
108 108
			if(content) {
109 109
				if($.isFunction(content)) {
110 110
					hand = content;
@ -151,8 +151,46 @@ $.define(["jQuery", "body", "win","doc"], "util", function($, $body, win,doc) {
151 151
					noop);
152 152
			}
153 153
		},
154
		serInArray=function(val,key,ret){
155
			var t = $.type(val);
156
			if("boolean"===t){
157
				ret.push(key+"="+(val?"1":"0"));
158
			}else if("string"=== t){
159
				if(val){
160
					ret.push(key+"="+encodeURIComponent(val));
161
				}
162
			}else if("number"=== t){
163
				ret.push(key+"="+val);
164
			}else{
165
				ret.push(key+"="+encodeURIComponent(JSON.stringify(val)));
166
			}
167
		},
168
		serialize=function(obj){
169
			var ret =[];
170
			if(obj){
171
			for(key in obj){
172
				var val = obj[key];
173
				var t = $.type(val);
174
				if("boolean"===t){
175
					ret.push(key+"="+(val?"1":"0"));
176
				}else if("string"=== t){
177
					if(val){
178
						ret.push(key+"="+encodeURIComponent(val));
179
					}
180
				}else if("number"=== t){
181
					ret.push(key+"="+val);
182
				}else if("array"===t){
183
					val.forEach(function(item){
184
						serInArray(item,key,ret);
185
					});
186
				}else if("object"=== t){
187
					ret.push(key+"="+encodeURIComponent(JSON.stringify(val)));
188
				}
189
			}}
190
			return ret.join("&");
191
		},
154 192
		g_def_err_hand = function(ep) {
155
			fem("http access error:\r\n" + JSON.stirngify(ep));
193
			errMsg("http access error:\r\n" + JSON.stirngify(ep));
156 194
		},
157 195
		g_err = {
158 196
			"defErrHand": function(ep) {
@ -164,28 +202,29 @@ $.define(["jQuery", "body", "win","doc"], "util", function($, $body, win,doc) {
164 202
		 * ep={code:"",msg:"",detailMsg:"",url:""} pa=function(code,msg,detailMsg)
165 203
		 * pa=boolean
166 204
		 */
167
		ajaxErrHand = function(ep, eh) {
168
			if(eh) {
169
				var pt = $.type(eh);
205
		ajaxErrHand = function(errparam, errHand) {
206
			if(errHand) {
207
				var pt = $.type(errHand);
170 208
				if(pt == "booean") {
171
					g_def_err_hand(ep);
209
					g_def_err_hand(errparam);
172 210
				} else if(pt == "function") {
173
					eh(ep);
211
					errHand(errparam);
174 212
				} else {
175
					var ph = eh[ep.code] || g_err[ep.code] || eh["defErrHand"] ||
213
					var ph = errHand[errparam.code] || g_err[errparam.code] || errHand["defErrHand"] ||
176 214
						g_err["defErrHand"];
177 215
					if(typeof ph === "string") {
178
						fem(ph);
216
						errMsg(ph);
179 217
					} else {
180
						ph(ep);
218
						ph(errparam);
181 219
					}
182 220
				}
183 221
			}
184 222
		},
185 223
		ajaxAccess = function(method, pUrl, pData, sh, eh, config) {
186 224
			config = config || {};
187
			if(false !== config.mask)
225
			if(false !== config.mask){
188 226
				loading();
227
			}
189 228
			config.traditional = true;
190 229
			config.type = method;
191 230
			config.url = pUrl, config.data = pData;
@ -255,11 +294,11 @@ $.define(["jQuery", "body", "win","doc"], "util", function($, $body, win,doc) {
255 294
		listModalIndex: function() {
256 295
			return lay_curr.index + 1;
257 296
		},
258
		error: fem,
259
		warn: fwm,
260
		msg: fm,
261
		alert: am,
262
		boxMsg: bm,
297
		error: errMsg,
298
		warn: warnMsg,
299
		msg: msg,
300
		alert: alertMsg,
301
		boxMsg: boxMsg,
263 302
		confirm: function(msg, yes, no) {
264 303
			bm({
265 304
				content: msg,
@ -273,17 +312,17 @@ $.define(["jQuery", "body", "win","doc"], "util", function($, $body, win,doc) {
273 312
			});
274 313
		},
275 314
		get: function(url, data, sh, eh, config) {
276
			ajaxAccess("get", url, data, sh, eh, config);
315
			ajaxAccess("GET", url, data, sh, eh, config);
277 316
		},
278 317
		post: function(url, data, sh, eh, config) {
279
			ajaxAccess("post", url, data, sh, eh, config);
318
			ajaxAccess("POST", url, data, sh, eh, config);
280 319
		},
281 320
		put: function(url, data, sh, eh, config) {
282
			ajaxAccess("post", url, data ? JSON.stringify(data) : "", sh, eh,
321
			ajaxAccess("PUT", url, data ? JSON.stringify(data) : null, sh, eh,
283 322
				config);
284 323
		},
285 324
		del: function(url, sh, eh, pObj) {
286
			ajaxAccess("post", url, null, sh, eh, pOjb);
325
			ajaxAccess("DELETE", url, null, sh, eh, pOjb);
287 326
		},
288 327
		noop: noop,
289 328
		nochange:function(d){return d},
@ -327,6 +366,6 @@ $.define(["jQuery", "body", "win","doc"], "util", function($, $body, win,doc) {
327 366
			buildElement(docf, obj);
328 367
			e.appendChild(docf);
329 368
		},
330
369
		serialize:serialize
331 370
	};
332 371
});