Skip to content

Commit d606b5a

Browse files
author
zhangxinxu
committed
some improvements
1. new better hash mechanism; 2. better ReadMe layout; 3. Github unsupport *.json, *.template formate
1 parent bc9371a commit d606b5a

File tree

8 files changed

+22
-23
lines changed

8 files changed

+22
-23
lines changed

ReadMe.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ Single Page Switching bone for mobile web APP, Hybird APP, Phonegap, ...
44

55
Why need this?
66
-----------------
7-
Better user experience for scene switching as native APP.
8-
1. Phonegap that to native APP is a single index.html, We need the same switch effects as native.
9-
2. Hybird app, as you know, mixed web-app and native-app. So, it' better that they have some switching experience.
7+
Better user experience for scene switching as native APP. <br>
8+
1. Phonegap that to native APP is a single index.html, We need the same switch effects as native.<br>
9+
2. Hybird app, as you know, mixed web-app and native-app. So, it' better that they have some switching experience.<br>
1010
3. Even no refresh interaction is not something bad for mobile APP.
1111

1212
How to use?
@@ -46,9 +46,9 @@ mobilebone.js
4646

4747
为何需要?
4848
-------------
49-
类原生APP的过场体验,适用于这些场景:
50-
1. Phonegap等类似跨移动开发平台,其静态页面都是index.html, 单页面,因此,需要跟原生一样的过场体验。
51-
2. Hybird app开发,原生APP内嵌web APP, 为了两者体验一致,不至于交互太唐突,也需要无刷新过场效果。
49+
类原生APP的过场体验,适用于这些场景:<br>
50+
1. Phonegap等类似跨移动开发平台,其静态页面都是index.html, 单页面,因此,需要跟原生一样的过场体验。<br>
51+
2. Hybird app开发,原生APP内嵌web APP, 为了两者体验一致,不至于交互太唐突,也需要无刷新过场效果。<br>
5252
3. 就算是纯粹的移动web APP, 使用无刷新模式也不失为一种不错的选项策略。
5353

5454

src/mobilebone.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@
224224
}
225225
if (supportHistory && this.pushStateEnabled && options.history !== false && url_push) {
226226
// if only pageIn, use 'replaceState'
227-
history[pageOut? "pushState": "replaceState"](null, document.title, url_push);
227+
history[pageOut? "pushState": "replaceState"](null, document.title, url_push.replace(/^#/, "#&"));
228228
}
229229

230230
// store page-id, just once
@@ -599,13 +599,14 @@
599599

600600
/**
601601
* Initialization. Load page according to location.hash. And bind link-catch events.
602-
*
603602
**/
604603
Mobilebone.init = function() {
605-
var hash = location.hash, ele_in = hash && document.querySelector(hash);
606-
if (hash == "") {
604+
var hash = location.hash.replace("#&", "#"), ele_in = null;
605+
if (hash == "" || hash == "#") {
607606
this.transition(document.querySelector("." + this.classPage));
608-
} else if (ele_in == null || ele_in.classList.contains(this.classPage) == false/* not page */) {
607+
} else if ((ele_in = document.querySelector(hash)) && ele_in.classList.contains(this.classPage)) { // 'ele_in' must be a page element
608+
this.transition(ele_in);
609+
} else {
609610
// as a ajax
610611
this.ajax({
611612
url: hash.replace("#", ""),
@@ -615,20 +616,18 @@
615616
Mobilebone.transition(ele_in);
616617
}
617618
});
618-
} else {
619-
this.transition(ele_in);
620-
}
619+
}
621620

622-
var eventName = "click";
623-
if (root.$ && root.$.fn && root.$.fn.tap) eventName = "tap";
621+
// Initialization link-catch events.
622+
var eventName = "click", $ = root.$ || root.jQuery || root.Zepto;
623+
if ($ && $.fn && $.fn.tap) eventName = "tap";
624624
if (this.captureLink == true) {
625625
document.addEventListener(eventName, this.handleTapEvent);
626626
}
627627
};
628628

629629
/**
630630
* If 'a' element has href, slide auto when tapping~
631-
*
632631
**/
633632
Mobilebone.handleTapEvent = function(event) {
634633
// get target and href
@@ -749,7 +748,7 @@
749748
**/
750749
if (supportHistory) {
751750
window.addEventListener("popstate", function() {
752-
var hash = location.hash.replace("#", "");
751+
var hash = location.hash.replace("#&", "").replace("#", "");
753752
if (hash == "") return;
754753

755754
var page_in = store[hash] || document.querySelector(location.hash), page_out = document.querySelector(".in." + Mobilebone.classPage);

test/ajax-json/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111
<body>
1212
<div id="pageHome" class="page out" data-title="ajax请求JSON">
1313
<ul>
14-
<li><a href="json-success.txt" data-dataType="json" data-ajax="true" data-error="requestError">点击加载JSON数据, github会挂</a></li>
14+
<li><a href="json-success.html" data-dataType="json" data-ajax="true" data-error="requestError">点击加载JSON数据</a></li>
1515
</ul>
1616
<ul>
17-
<li><a href="json-error.html" data-datatype="json" data-error="requestError">加载JSON失败,404, 可回调</a></li>
18-
<li><a href="json-error.txt" data-datatype="json" data-error="requestError">加载JSON失败,解析错误</a></li>
17+
<li><a href="json-error.txt" data-datatype="json" data-error="requestError">加载JSON失败,404, 可回调</a></li>
18+
<li><a href="json-error.html" data-datatype="json" data-error="requestError">加载JSON失败,解析错误</a></li>
1919
</ul>
2020
<ul>
2121
<li><a href="../index.html" data-ajax="false">&laquo; 返回测试引导首页</a></li>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

test/backbone/index.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<body>
1515
<div id="homePage" class="page out" data-title="backbone模板框架结合示例">
1616
<ul>
17-
<li><a href="data.json" data-datatype="json">大列表展示</a></li>
17+
<li><a href="data.html" data-datatype="json">大列表展示</a></li>
1818
</ul>
1919
<ul>
2020
<li><a href="../index.html" data-ajax="false">&laquo; 返回测试引导首页</a></li>
@@ -46,7 +46,7 @@
4646

4747
// 获得模板内容
4848
var xhr = new XMLHttpRequest();
49-
xhr.open("GET", key + ".template");
49+
xhr.open("GET", key + ".html");
5050
xhr.onload = function() {
5151
var FindView = Backbone.View.extend({
5252
initialize: function() {

0 commit comments

Comments
 (0)