Skip to content

Commit cc297f5

Browse files
committed
Merge pull request #32 from zhangxinxu/develop
Develop
2 parents 05430ed + 4c99ac6 commit cc297f5

File tree

5 files changed

+39
-19
lines changed

5 files changed

+39
-19
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.0.1",
3+
"version": "2.0.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: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
*
4141
* @type string
4242
**/
43-
Mobilebone.VERSION = '2.0.1';
43+
Mobilebone.VERSION = '2.0.2';
4444

4545
/**
4646
* Whether catch attribute of href from element with tag 'a'
@@ -175,13 +175,13 @@
175175
var local_function_key = element.getAttribute("data-" + key) || _dataparams[key];
176176
if (typeof _params.root[local_function_key] == "function") {
177177
_params[key] = function() {
178-
defaults[key].apply(null, arguments);
179-
_params.root[local_function_key].apply(null, arguments);
178+
defaults[key].apply(this, arguments);
179+
_params.root[local_function_key].apply(this, arguments);
180180
}
181181
} else if (typeof options[key] == "function") {
182182
_params[key] = function() {
183-
defaults[key].apply(null, arguments);
184-
options[key].apply(null, arguments);
183+
defaults[key].apply(this, arguments);
184+
options[key].apply(this, arguments);
185185
}
186186
} else {
187187
_params[key] = defaults[key];
@@ -208,7 +208,7 @@
208208
// do fallback every time
209209
var fallback = params_out.fallback;
210210
if (typeof fallback == "string") fallback = params_out.root[fallback];
211-
if (typeof fallback == "function") fallback(pageInto, pageOut, options.response);
211+
if (typeof fallback == "function") fallback.call(params_out.root, pageInto, pageOut, options.response);
212212
}
213213
if (pageInto != null && pageInto.classList) {
214214
// for title change
@@ -302,8 +302,9 @@
302302

303303
// do callback every time
304304
var callback = params_in.callback;
305+
305306
if (typeof callback == "string") callback = params_in.root[callback];
306-
if (typeof callback == "function") callback(pageInto, pageOut, options.response);
307+
if (typeof callback == "function") callback.call(params_in.root, pageInto, pageOut, options.response);
307308
}
308309
};
309310

@@ -726,7 +727,8 @@
726727
if (hasInited == true) return 'Don\'t repeat initialization!';
727728
var hash = location.hash.replace("#&", "#"), ele_in = null;
728729
if (hash == "" || hash == "#") {
729-
this.transition(document.querySelector("." + this.classPage));
730+
store._initPage = document.querySelector("." + this.classPage);
731+
this.transition(store._initPage);
730732
} else if (isSimple.test(hash) == true && (ele_in = document.querySelector(hash)) && ele_in.classList.contains(this.classPage)) { // 'ele_in' must be a page element
731733
this.transition(ele_in);
732734
} else {
@@ -908,10 +910,14 @@
908910
* page change when history change
909911
**/
910912
window.addEventListener("popstate", function() {
911-
var hash = location.hash.replace("#&", "").replace("#", "");
912-
if (hash == "") return;
913-
914-
var page_in = store[hash];
913+
var hash = location.hash.replace("#&", "").replace("#", ""), page_in = null;
914+
if (hash == "") {
915+
// if no hash, get init page as 'page_in'
916+
page_in = store._initPage;
917+
if (!page_in) return;
918+
} else {
919+
page_in = store[hash];
920+
}
915921

916922
if (!page_in) {
917923
if(isSimple.test(hash) == false) {

test/callback/index.html

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ <h2>主页</h2>
2626
</ul>
2727
<p></p>
2828
</div>
29-
<div id="page1" class="page out" data-params="animationstart=start&amp;animationend=end">
29+
<div id="page1" class="page out" data-callback="callback" data-params="animationstart=start&amp;animationend=end">
3030
<a href="#pageHome" data-rel="back">&laquo;返回1</a>
3131
<p></p>
3232
</div>
@@ -55,6 +55,9 @@ <h2>主页</h2>
5555
into: "进入",
5656
out: "离开"
5757
},
58+
callback: function() {
59+
console.log(this.status.into = "回调执行!this对应的是Fun!");
60+
},
5861
home: function(pageInto, pageOut, response) {
5962
pageInto.querySelector("p").innerHTML += "首次载入时间是:" + this.time() + "<br>";
6063
},
@@ -65,6 +68,17 @@ <h2>主页</h2>
6568
page.querySelector("p").innerHTML += "页面"+ this.status[into_or_out] +"动画完成,时间是:" + this.time() + "<br>";
6669
}
6770
};
71+
// 2014-12-28新增回调合并时候的上下文测试
72+
Mobilebone.callback = function() {
73+
// 上下文为当前page回调所在的对象,例如本测试页面:
74+
// 1. 默认为Fun;
75+
// 2. page2因为root=window, 因此上下文为window
76+
// 3. 页面3则是 callback_page.test_obj
77+
// 需要注意的是,如果存在回调合并的情况,其公用同一个上下文this, 值同上规则
78+
console.log("全局回调上下文this: ");
79+
console.log(this);
80+
};
81+
6882

6983
window.callback_page = function(page) {
7084
// 按钮居中

test/history/ajax-page.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
</head>
66

77
<body>
8-
<div class="page out"><a href="#pageHome" data-rel="back">返回</a></div>
8+
<div class="page out"><a href="javascript:" data-rel="back">返回</a></div>
99
</body>
1010
</html>

test/history/history.html

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

1111
<body>
12-
<div id="pageHome" class="page out">
12+
<div class="page out">
1313
<ul>
1414
<li><a href="#page1">浏览器返回测试-页面1</a></li>
1515
<li><a href="#page2">浏览器返回测试-页面2</a></li>
@@ -22,9 +22,9 @@
2222
<li><a href="../index.html" data-ajax="false">&laquo; 返回测试引导首页</a></li>
2323
</ul>
2424
</div>
25-
<div id="page1" class="page out"><a href="#pageHome" data-rel="back">&laquo;返回1</a></div>
26-
<div id="page2" class="page out"><a href="#pageHome" data-rel="back">&laquo;返回2</a></div>
27-
<div id="page3" class="page out"><a href="#pageHome" data-rel="back">&laquo;返回3</a></div>
25+
<div id="page1" class="page out"><a href="javascript:" data-rel="back">&laquo;返回1</a></div>
26+
<div id="page2" class="page out"><a href="javascript:" data-rel="back">&laquo;返回2</a></div>
27+
<div id="page3" class="page out"><a href="javascript:" data-rel="back">&laquo;返回3</a></div>
2828

2929
<script src="../../src/mobilebone.js"></script>
3030
</body>

0 commit comments

Comments
 (0)