Skip to content

Commit 26bc1fa

Browse files
author
zhangxinxu
committed
two big improvements
1. fix form get no data submit bug 2. sperate hashid and pageid
1 parent f8622d5 commit 26bc1fa

File tree

3 files changed

+86
-20
lines changed

3 files changed

+86
-20
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mobilebone",
3-
"version": "2.4.1",
3+
"version": "2.4.2",
44
"description": "Bone main for mobile web APP with a sigle page mode.",
55
"main": "src/mobilebone.js",
66
"directories": {

src/mobilebone.js

Lines changed: 62 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
*
3939
* @type string
4040
**/
41-
Mobilebone.VERSION = '2.4.1';
41+
Mobilebone.VERSION = '2.4.2';
4242

4343
/**
4444
* Whether catch attribute of href from element with tag 'a'
@@ -274,7 +274,10 @@
274274
}
275275

276276
// delete page with same id when options.remove !== false
277-
var pageid = options.id || pageInto.id;
277+
var pageid = options.id || pageInto.id, hashid = options.id || pageInto.id;
278+
if (options.id && options.reload == "root") {
279+
pageid = pageid.split("?")[0];
280+
}
278281

279282
if (options.remove !== false && store[pageid] && store[pageid] != pageInto && store[pageid].parentElement) {
280283
store[pageid].parentElement.removeChild(store[pageid]);
@@ -336,7 +339,10 @@
336339
});
337340

338341
// history
339-
var url_push = pageid, url_push_replaced = '';
342+
// hashid should a full url address
343+
// different with pageid
344+
// add on 2.4.2
345+
var url_push = hashid, url_push_replaced = '';
340346
if (url_push && /^#/.test(url_push) == false) {
341347
url_push = "#" + url_push;
342348
}
@@ -383,6 +389,7 @@
383389
params: string|object. ajax params. - Optional
384390
* @returns string
385391
* @example Mobilebone.getCleanUrl(elementOfA);
392+
Mobilebone.getCleanUrl(elementOfForm);
386393
Mobilebone.getCleanUrl(elementOfA, '', "a=1&b=2");
387394
Mobilebone.getCleanUrl(null, "xxx.html");
388395
Mobilebone.getCleanUrl(null, "xxx.html?a=1&b=2");
@@ -393,10 +400,39 @@
393400
if (trigger) {
394401
if (trigger.nodeType == 1) {
395402
// form element
396-
if (trigger.action) return trigger.getAttribute("action");
397-
// a element
398-
href = trigger.getAttribute("href");
399-
formdata = trigger.getAttribute("data-formdata") || trigger.getAttribute("data-data");
403+
if (trigger.action) {
404+
href = trigger.getAttribute("action");
405+
// add on v2.4.1
406+
if (trigger.method && trigger.method.toUpperCase() == "POST") {
407+
return href;
408+
} else if (window.$ && $.fn && $.fn.serialize) {
409+
// use jquery serialize()
410+
formdata = $(trigger).serialize();
411+
} else {
412+
formdata = {};
413+
// simple serialize from Mobilebone
414+
slice.call(trigger.querySelectorAll("input,select,textarea")).forEach(function(control) {
415+
if (control.name && !control.disabled) {
416+
var val = control.value.trim(), name = control.name;
417+
if (/^radio|checkbox/i.test(control.type)) {
418+
if (control.checked) {
419+
if (formdata[name]) {
420+
formdata[name].push(val);
421+
} else {
422+
formdata[name] = [val];
423+
}
424+
}
425+
} else {
426+
formdata[name] = [val];
427+
}
428+
}
429+
});
430+
}
431+
} else {
432+
// a element
433+
href = trigger.getAttribute("href");
434+
formdata = trigger.getAttribute("data-formdata") || trigger.getAttribute("data-data");
435+
}
400436
} else if (trigger.url) {
401437
href = trigger.url;
402438
formdata = trigger.data;
@@ -407,11 +443,17 @@
407443

408444
// get formdata
409445
formdata = formdata || params || "";
410-
446+
console.dir(formdata);
411447
if (typeof formdata == "object") {
412448
var arr_data = [];
413-
for (key in params) {
414-
arr_data.push(key + "=" + encodeURIComponent(params[key]));
449+
for (key in formdata) {
450+
if (!formdata[key].forEach) {
451+
formdata[key] = [formdata[key]];
452+
}
453+
formdata[key].forEach(function(keyValue) {
454+
arr_data.push(key + "=" + encodeURIComponent(keyValue));
455+
});
456+
415457
}
416458
if (arr_data.length > 0) {
417459
formdata = arr_data.join("&");
@@ -526,8 +568,9 @@
526568
// do transition
527569
optionsTransition.response = response || dom_or_html;
528570
optionsTransition.id = this.getCleanUrl(element_or_options) || create_page.id || ("unique" + Date.now());
571+
529572
if (is_root == true) {
530-
optionsTransition.id = optionsTransition.id.split("?")[0];
573+
optionsTransition.reload = "root";
531574
}
532575
// 'if' statement below added on v2.0.0
533576
if (typeof options == "object") {
@@ -622,7 +665,8 @@
622665
}
623666
}
624667
}
625-
668+
669+
// ajax的url地址
626670
params.url = this.getCleanUrl(trigger_or_options, params.url);
627671
params.target = trigger_or_options;
628672

@@ -672,7 +716,7 @@
672716
ele_mask.style.visibility = "visible";
673717

674718
// ajax request
675-
var xhr = new XMLHttpRequest();
719+
var xhr = new XMLHttpRequest();
676720
xhr.open(params.type || "GET", params.url + (/\?/.test(params.url)? "&" : "?") + "r=" + Date.now(), params.async, params.username, params.password);
677721
xhr.timeout = params.timeout;
678722

@@ -756,6 +800,11 @@
756800
var ajax = form.getAttribute("data-ajax");
757801
if (ajax == "false" || (this.captureForm == false && ajax != "true")) return;
758802

803+
// All form no cache
804+
if (form.getAttribute("data-reload") === null) {
805+
form.setAttribute("data-reload", "root");
806+
}
807+
759808
form.addEventListener("submit", function(event) {
760809
// prevent detect
761810
var attrPrevent = this.getAttribute("data-preventdefault");

test/form-submit/index.html

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,37 @@
88
<link rel="stylesheet" href="../test.css">
99
<link rel="stylesheet" href="zxx.lib.css">
1010
<style>
11-
input { font-size: 100%; }
12-
input[name] {
11+
[name], .submit {
1312
height: 40px; width: 100%; padding: 10px; box-sizing: border-box; font-size: 100%;
1413
}
15-
.search { height: 40px; padding: 10px; box-sizing: border-box; }
16-
.submit { height: 40px; font-size: 16px; padding: 0 25px; }
14+
[type=radio], [type=checkbox] {
15+
width: auto; height: auto;
16+
}
1717
</style>
1818
</head>
1919

2020
<body>
2121
<div id="pageHome" class="page out">
2222
<ul>
23-
<li class="pl10">
23+
<li class="pl10 pr10">
2424
<form method="get" action="search.html">
25-
<input type="search" class="search mr10" required><input type="submit" class="submit" value="搜索">
25+
<p><select name="city" required>
26+
<option value="1">上海</option>
27+
<option value="1">北京</option>
28+
</select></p>
29+
<p><textarea name="mark">标注内容</textarea></p>
30+
<p>
31+
<input type="radio" name="radio" value="1"><label class="mr20">单选1</label>
32+
<input type="radio" name="radio" value="2"><label>单选2</label>
33+
</p>
34+
<p>
35+
<input type="checkbox" name="check" value="1"><label class="mr20">复选1</label>
36+
<input type="checkbox" name="check" value="2" checked><label class="mr20">复选2</label>
37+
<input type="checkbox" name="check" value="3" checked><label class="mr20">复选3</label>
38+
<input type="checkbox" name="check" value="4"><label class="mr20">复选4</label>
39+
</p>
40+
<p><input type="search" class="search mr10" name="key" required></p>
41+
<p><input type="submit" class="submit" value="搜索"></p>
2642
</form>
2743
</li>
2844
</ul>
@@ -35,5 +51,6 @@
3551
</div>
3652

3753
<script src="../../src/mobilebone.js"></script>
54+
<!--<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>-->
3855
</body>
3956
</html>

0 commit comments

Comments
 (0)