Skip to content

Commit c91101a

Browse files
author
zhangxinxu
committed
fix ajax back direction bug
fix ajax back direction bug
1 parent 1e04e84 commit c91101a

File tree

5 files changed

+54
-35
lines changed

5 files changed

+54
-35
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.5.1",
3+
"version": "2.5.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: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
*
4444
* @type string
4545
**/
46-
Mobilebone.VERSION = '2.5.1';
46+
Mobilebone.VERSION = '2.5.2';
4747

4848
/**
4949
* Whether catch attribute of href from element with tag 'a'
@@ -159,8 +159,9 @@
159159
if (arguments.length == 0 || pageInto == pageOut) return;
160160
if (arguments.length == 3 && isNaN(back * 1) == true) {
161161
options = back;
162-
back = false;
162+
back = options.back;
163163
};
164+
164165
//if those parameters is missing
165166
pageOut = pageOut || null, back = back || false, options = options || {};
166167

@@ -535,6 +536,7 @@
535536

536537
// get page-title from eleOrObj or options
537538
var page_title, id_container, classPageInside;
539+
538540
if (eleOrObj) {
539541
if (eleOrObj.nodeType == 1) {
540542
// legal elements
@@ -552,12 +554,18 @@
552554
if (eleOrObj.tagName.toLowerCase() == "form" || (isreload !== null && isreload != "false")) {
553555
optionsTransition.reload = true;
554556
}
557+
// v2.5.2
558+
// is back? for issues #128
559+
optionsTransition.back = eleOrObj.getAttribute("data-rel") == "back";
555560
} else {
556561
response = eleOrObj.response || options.response;
557562
page_title = eleOrObj.title || options.title;
558563
container = eleOrObj.container || options.container;
559564
classPageInside = eleOrObj.classPage || options.classPage;
560565
optionsTransition.target = eleOrObj.target;
566+
// v2.5.2
567+
// is back? for issues #128
568+
optionsTransition.back = eleOrObj.back || options.back;
561569
}
562570
if (container && classPageInside) classPage = classPageInside;
563571
}
@@ -659,7 +667,7 @@
659667
/**
660668
* For ajax request to get HTML or JSON.
661669
662-
* @params trigger_or_options - Necessary
670+
* @params aOrFormOrObj - Necessary
663671
1. dom-object:<a>|<form>.
664672
2. object.
665673
* @returns undefined
@@ -670,8 +678,8 @@
670678
});
671679
*
672680
**/
673-
Mobilebone.ajax = function(trigger_or_options) {
674-
if (!trigger_or_options) return;
681+
Mobilebone.ajax = function(aOrFormOrObj) {
682+
if (!aOrFormOrObj) return;
675683

676684
// default params
677685
var defaults = {
@@ -690,14 +698,14 @@
690698

691699
var params = {}, ele_mask = null, formData = null;
692700

693-
// if 'trigger_or_options' is a element, we should turn it to options-object
701+
// if 'aOrFormOrObj' is a element, we should turn it to options-object
694702
var params_from_trigger = {}, attr_mask;
695-
if (trigger_or_options.nodeType == 1) {
696-
params_from_trigger = _queryToObject(trigger_or_options.getAttribute("data-params") || "");
703+
if (aOrFormOrObj.nodeType == 1) {
704+
params_from_trigger = _queryToObject(aOrFormOrObj.getAttribute("data-params") || "");
697705
// get params
698706
for (key in defaults) {
699707
// data-* > data-params > defaults
700-
params[key] = trigger_or_options.getAttribute("data-" + key) || params_from_trigger[key] || defaults[key];
708+
params[key] = aOrFormOrObj.getAttribute("data-" + key) || params_from_trigger[key] || defaults[key];
701709
if (typeof defaults[key] == "function" && typeof params[key] == "string") {
702710
// eg. globalObject.functionName
703711
params[key] = this.getFunction(params[key]);
@@ -707,33 +715,40 @@
707715
}
708716
}
709717

710-
// ajax的url地址
711-
params.url = this.getCleanUrl(trigger_or_options, params.url);
712-
params.target = trigger_or_options;
718+
// address of ajax url
719+
params.url = this.getCleanUrl(aOrFormOrObj, params.url);
720+
params.target = aOrFormOrObj;
721+
// v2.5.2
722+
// is back? for issues #128
723+
params.back = aOrFormOrObj.getAttribute("data-rel") == "back";
713724

714-
var tagName = trigger_or_options.tagName.toLowerCase();
725+
var tagName = aOrFormOrObj.tagName.toLowerCase();
715726
if (tagName == "form") {
716-
params.type = trigger_or_options.method;
727+
params.type = aOrFormOrObj.method;
717728

718-
formData = new FormData(trigger_or_options);
729+
formData = new FormData(aOrFormOrObj);
719730
}
720731

721732
// get mask element
722-
attr_mask = trigger_or_options.getAttribute("data-mask");
733+
attr_mask = aOrFormOrObj.getAttribute("data-mask");
723734
if (attr_mask == "true" || attr_mask == "") {
724-
ele_mask = trigger_or_options.querySelector("." + this.classMask);
735+
ele_mask = aOrFormOrObj.querySelector("." + this.classMask);
725736
}
726737
}
727-
// if 'trigger_or_options' is a object
728-
else if (trigger_or_options.url) {
738+
// if 'aOrFormOrObj' is a object
739+
else if (aOrFormOrObj.url) {
729740
// get params
730741
for (key2 in defaults) {
731-
params[key2] = trigger_or_options[key2] || defaults[key2];
742+
params[key2] = aOrFormOrObj[key2] || defaults[key2];
732743
}
733744
// get url
734745
params.url = this.getCleanUrl(null, params.url, params.data);
735746
// here params.title will become page title;
736-
params.title = trigger_or_options.title;
747+
params.title = aOrFormOrObj.title;
748+
// v2.5.2
749+
// is back? for issues #128
750+
// when history.back()
751+
params.back = aOrFormOrObj.back;
737752
} else {
738753
return;
739754
}
@@ -748,7 +763,7 @@
748763
ele_mask.className = this.classMask;
749764
ele_mask.innerHTML = '<i class="loading"></i>';
750765
if (typeof attr_mask == "string") {
751-
trigger_or_options.appendChild(ele_mask);
766+
aOrFormOrObj.appendChild(ele_mask);
752767
} else {
753768
document.body.appendChild(ele_mask);
754769
}
@@ -770,7 +785,7 @@
770785
try {
771786
response = JSON.parse(xhr.response);
772787
params.response = response;
773-
Mobilebone.createPage(Mobilebone.jsonHandle(response), trigger_or_options, params);
788+
Mobilebone.createPage(Mobilebone.jsonHandle(response), aOrFormOrObj, params);
774789
} catch (e) {
775790
params.message = "JSON parse error:" + e.message;
776791
params.error.call(params, xhr, xhr.status);
@@ -785,16 +800,16 @@
785800
// as json
786801
response = JSON.parse(xhr.response);
787802
params.response = response;
788-
Mobilebone.createPage(Mobilebone.jsonHandle(response), trigger_or_options, params);
803+
Mobilebone.createPage(Mobilebone.jsonHandle(response), aOrFormOrObj, params);
789804
} catch (e) {
790805
// as html
791806
response = xhr.response;
792-
Mobilebone.createPage(response, trigger_or_options, params);
807+
Mobilebone.createPage(response, aOrFormOrObj, params);
793808
}
794809
} else {
795810
response = xhr.response;
796811
// 'response' is string
797-
Mobilebone.createPage(response, trigger_or_options, params);
812+
Mobilebone.createPage(response, aOrFormOrObj, params);
798813
}
799814
params.success.call(params, response, xhr.status);
800815
} else {
@@ -949,10 +964,9 @@
949964
if (target.getAttribute("data-rel") == "external"
950965
|| ajax == "false"
951966
|| (href.replace("://", "").split("/")[0] !== location.href.replace("://", "").split("/")[0] && ajax != "true")
952-
|| (Mobilebone.captureLink == false && ajax != "true")
953967
) return;
954968
event.preventDefault();
955-
});
969+
});
956970
}
957971
}
958972
// Important:
@@ -1185,7 +1199,8 @@
11851199
// as a url
11861200
Mobilebone.ajax({
11871201
url: hash,
1188-
dataType: "unknown"
1202+
dataType: "unknown",
1203+
back: Mobilebone.isBack()
11891204
});
11901205
return;
11911206
}

test/ajax-html/c.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
<div class="page out" data-title="直接访问我试试">
99
<ul>
1010
<li><a href="#pageHome" data-rel="back">c页面,浏览器返回</a></li>
11-
<li><a href="" data-rel="back">再次加重c页面</a></li>
11+
<li><a href="" data-rel="back">再次加载c页面</a></li>
1212
<li><a href="javascript:" data-rel="back">直接返回b页面</a></li>
13+
<li><a href="b.html" data-rel="back">有Ajax地址,有data-rel="back"</a></li>
1314
</ul>
1415
</div>
1516
</body>

test/ajax-html/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
<script src="../../src/mobilebone.js"></script>
4444
<script src="../../src/mobilebone.js"></script>
4545
<!-- 厂子的frozenjs兼容测试 -->
46-
<script src="../base-slide/zepto.js"></script>
47-
<script src="../base-slide/frozen.js"></script>
46+
<!--<script src="../base-slide/zepto.js"></script>
47+
<script src="../base-slide/frozen.js"></script>-->
4848
<script>
4949
var optionsTest = function(elein, eleout, options) {
5050
// ajax过场回调options参数测试

test/base-slide/index.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
</head>
1010

1111
<body>
12-
<div id="pageHome" class="page out">
12+
<div id="pageHome" class="page">
1313
<ul>
1414
<li><a href="#&page1">页面1-#&amp;page1</a></li>
1515
<li><a href="#page2">页面2-#page2</a></li>
1616
<li><a href="#page3">页面3</a></li>
1717
<li><a>测试菊花:<div class="mask"><i class="loading"></i></div></a></li>
1818
</ul>
1919
<ul>
20-
<li><a href="../index.html" data-ajax="false">&laquo; 返回测试引导首页</a></li>
20+
<li><a href="../index.html" id="back" data-ajax="false">&laquo; 返回测试引导首页</a></li>
2121
<li id="result"></li>
2222
</ul>
2323
</div>
@@ -39,6 +39,9 @@
3939
/*console.log = function(content) {
4040
document.getElementById("result").innerHTML += '<p>'+ content +'</p>';
4141
};*/
42+
$("#back").on("tap", function() {
43+
location.href = this.href;
44+
});
4245
</script>
4346
</body>
4447
</html>

0 commit comments

Comments
 (0)