Skip to content

Commit 3b4803c

Browse files
committed
Merge pull request #139 from zhangxinxu/develop
Develop
2 parents c2d07f3 + 2af8a8c commit 3b4803c

8 files changed

+110
-29
lines changed

docs/Mobilebone.createPage.html

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,29 @@ <h3>参数</h3>
1818
<ul>
1919
<li><code>dom_or_html</code> DOM元素或字符串。必须参数。生成创建页面的主内容。</li>
2020
<li><code>element_or_options</code> DOM元素或者对象。可选参数,可以是<code>&lt;a></code>元素,page元素,也可以是第3个<code>options</code>参数。此参数主要用来获得触发元素上绑定的一些属性值,例如<code>data-title</code></li>
21-
<li><code>options</code> 键值序列对象。可选参数。此参数多内部触发,基本上来自Ajax请求的参数。主要作用是获得response返回数据。</li>
21+
<li><code>options</code> 键值序列对象。可选参数。</li>
2222
</ul>
2323
<h3>返回值</h3>
2424
<p>-</p>
25+
<h3>更多说明 <time>(add on 2015-05-04)</time></h3>
26+
<ol>
27+
<li><code>options</code>参数缺省的时候,第二个参数<code>element_or_options</code>可以作为<code>options</code>参数使用;</li>
28+
<li>目前,常用<code>options</code>选项有:
29+
<ul>
30+
<li><code>options.history</code> 是否增加历史记录;</li>
31+
<li><code>options.remove</code> 是否移除重复的页面元素;</li>
32+
<li><code>options.target</code> 点击触发过场的元素;</li>
33+
</ul>
34+
<p>其他选项,还包括:</p>
35+
<ul>
36+
<li><code>options.title</code> 新页面标题;</li>
37+
<li><code>options.container</code> 装载新页面的容器;</li>
38+
<li><code>options.classPage</code> 内切换的类名;</li>
39+
<li><code>options.back</code> 布尔型。是否过场是返回类型;</li>
40+
</ul>
41+
42+
</li>
43+
</ol>
2544
<h3>使用示例</h3>
2645
<pre>Mobilebone.createPage(pageDom);
2746
Mobilebone.createPage(generalDom);

docs/Mobilebone.evalScript.html

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,40 @@ <h3>使用</h3>
2121
<h3>效果</h3>
2222
<p>Ajax请求的HTML字符串中内联的script脚本会执行。</p>
2323
<h3>其他说明</h3>
24-
<ul>
24+
<ol>
2525
<li>只会执行内联JS脚本,<code>src</code>外链JS忽略。如下面会执行:
2626
<pre>&lt;script>console.log("script excuted!");&lt;/script></pre>
2727
<p>但是下面这个1.js会忽略:</p>
2828
<pre>&lt;script src="1.js">&lt;/script></pre>
29+
<p>如果你需要外链JS执行,可以使用内联JS做处理,例如:</p>
30+
<pre>var script = document.createElement("script");
31+
script.src = "a.js";
32+
document.body.appendChild(script);</pre>
2933
</li>
30-
</ul>
34+
<li><time>(add on 2015-05-04)</time> 关于脚本执行的时机。内联JavaScript在页面即将载入的一瞬间就会进行。注意,此时可能存在两个结构和ID均一致的页面,例如,从分页1页面切换到分页2页面,此时,例如,给某元素绑定事件,例如:
35+
<pre>document.getElementById("test").addEventListener("click", function() {
36+
console.log("clicked!");
37+
}); </pre>
38+
<p>则,绑定的事件可能在之前一个元素上。然后造成新页面元素没事件,之前页面元素重复绑定。</p>
39+
<p>避免此问题方法很多(测试页面<code>/test/ajax-html/index.html</code>均有示意):</p>
40+
<ol>
41+
<li>加一个延迟,例如350毫秒。</li>
42+
<li>使用Mobilebone内置回调方法,例如<code>animationend</code>, 或者<code>onpagefirstinto</code>的参数限制(如下):
43+
<pre>var fun_firstinto = function(pagein) {
44+
pagein.querySelector("#test").addEventListener("click", function() {
45+
console.log("clicked!");
46+
});
47+
}</pre>
48+
<p>这样,事件就准确绑定在对应页面元素上了!</p>
49+
</li>
50+
<li>
51+
或者使用动态id,如:<pre>id="test&lt;?php echo $_GET['id']; ?&gt;
52+
53+
document.getElementById("test&lt;?php echo $_GET['id']; ?&gt;")</pre>
54+
</li>
55+
</ol>
56+
</li>
57+
</ol>
3158
</div>
3259
</div>
3360

docs/data-callbackKeys.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ <h2>data-callbackKeys</h2>
1616
<p>这里的<code>callbackKeys</code>只是一种统称,实际没有这个自定义属性的,指的是一系列回调写法,因为除了长相不一样,其他都类似,所以合在一起讲了。</p>
1717
<p>其家庭成员包括:</p>
1818
<ul>
19-
<li>data-onpagefirstinto</li>
20-
<li>data-callback</li>
21-
<li>data-fallback</li>
22-
<li>data-animationstart</li>
23-
<li>data-animationend</li>
24-
<li>data-preventdefault</li>
19+
<li><code>data-onpagefirstinto</code> 页面第一次载入执行,过场刚开始即执行。</li>
20+
<li><code>data-callback</code> 页面每次进入都会执行,过场刚开始即执行。</li>
21+
<li><code>data-fallback</code> 页面每次离开都会执行,过场刚开始即执行。</li>
22+
<li><code>data-animationstart</code> 页面动画即将开始执行的回调,离开页面,进入页面均会触发。</li>
23+
<li><code>data-animationend</code> 页面动画结束之后执行的回调,离开页面,进入页面均会触发。</li>
24+
<li><code>data-preventdefault</code> 介入回调,可以用来中断Mobilebone的一些默认行为,再过场等行为触发之前实现一些自定义行为。</li>
2525
</ul>
2626
<p>每个回调的作用于使用场景可参见博文说明:<a href="http://www.zhangxinxu.com/wordpress/2014/10/mobilebone-js-mobile-web-app-core/" class="link">点击这里</a></p>
2727
<p>其中,<code>data-preventdefault</code>并非<code>page</code>元素专有,<code>a</code>元素,<code>form</code>元素也支持此回调,具体可<a href="data-preventDefault.html" class="link">点击这里</a></p>

docs/docs.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ body { line-height: 1.5; font-size: 14px; font-family: "Helvetica Neue", Helveti
44
-ms-touch-action: auto;
55
}
66
a { text-decoration: none; -webkit-tap-highlight-color: rgba(0,0,0,0); }
7+
time { opacity: .75; }
78
aside { position: absolute; left: 0; top: 0; bottom: 0; width: 250px; background-color: #0C121A; box-shadow: 2px 0 1px rgba(0,0,0,.3); color: #fff; overflow: auto; z-index: 1; }
89
aside > h2 { font-size: 1.2em; }
910
aside a { display: block; padding: 5px 10px; color: #fff; position: relative; }

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.4",
3+
"version": "2.5.5",
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: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
// Is it a id selector
3232
var isSimple = /^#?\w+(?:[\-_]\w+)*$/i;
3333

34+
// Is it webkit
35+
var isWebkit = 'WebkitAppearance' in document.documentElement.style || typeof document.webkitHidden != "undefined";
36+
3437
// Is it suppory history API
3538
var supportHistory = "pushState" in history && "replaceState" in history;
3639

@@ -43,7 +46,7 @@
4346
*
4447
* @type string
4548
**/
46-
Mobilebone.VERSION = '2.5.4';
49+
Mobilebone.VERSION = '2.5.5';
4750

4851
/**
4952
* Whether catch attribute of href from element with tag 'a'
@@ -262,6 +265,9 @@
262265
// if reverse direction
263266
pageOut.classList[back? "add": "remove"]("reverse");
264267

268+
// add on v2.5.5
269+
pageOut.removeSelf = null;
270+
265271
// do fallback every time
266272
var fallback = params_out.fallback;
267273
if (typeof fallback == "string") fallback = params_out.root[fallback];
@@ -295,16 +301,21 @@
295301
}
296302
var relid = store["_" + pageid];
297303
if (options.remove !== false && store[pageid] && store[pageid] != pageInto && store[pageid].parentElement) {
298-
store[pageid].parentElement.removeChild(store[pageid]);
299-
delete store[pageid];
300304
// hashid may store the same page, we should delete also
301305
// when data-reload not 'false' or null
302306
// v2.4.4+
303307
if (relid && store[relid] && options.reload == true) {
304308
delete store[relid];
305309
delete store["_" + pageid];
306310
}
307-
}
311+
if (store[pageid] != pageOut) {
312+
store[pageid].parentElement.removeChild(store[pageid]);
313+
} else {
314+
pageOut.removeSelf = true;
315+
}
316+
delete store[pageid];
317+
}
318+
308319

309320
// do transition
310321
if (pageOut) pageInto.classList.add(params_in.form);
@@ -320,7 +331,9 @@
320331

321332
// do callback when come in first time
322333
var onpagefirstinto = params_in.onpagefirstinto;
323-
if (!store[pageid] && !store[hashid]) {
334+
// first judge change to pageInto store
335+
// v2.5.5 add for fix issues #138
336+
if (!pageInto.firstintoBind) {
324337
if (typeof onpagefirstinto == "string" && params_in.root[onpagefirstinto]) {
325338
params_in.root[onpagefirstinto](pageInto, pageOut, options);
326339
} else if (typeof onpagefirstinto == "function") {
@@ -329,11 +342,12 @@
329342
// capture form submit
330343
slice.call(pageInto.querySelectorAll("form")).forEach(function(form) {
331344
Mobilebone.submit(form);
332-
});
345+
});
346+
347+
pageInto.firstintoBind = true;
333348
}
334349

335350
// do callback when animation start/end
336-
var isWebkit = 'WebkitAppearance' in document.documentElement.style || typeof document.webkitHidden != "undefined";
337351
["animationstart", "animationend"].forEach(function(animationkey, index) {
338352
var animition = params_in[animationkey], webkitkey = "webkit" + animationkey.replace(/^a|s|e/g, function(matchs) {
339353
return matchs.toUpperCase();
@@ -346,6 +360,12 @@
346360
this.style.display = "none";
347361
}
348362
this.classList.remove(params(this).form);
363+
364+
// add on v2.5.5
365+
if (this.removeSelf == true) {
366+
this.parentElement.removeChild(this);
367+
this.removeSelf = null;
368+
}
349369
});
350370
// bind animation events
351371
if (typeof animition == "string" && params_in.root[animition]) {
@@ -529,6 +549,9 @@
529549
// 'eleOrObj' can '.page element', or 'a element', or 'options'
530550
// basically, options = ajax options, of course, u can custom it!
531551
if (!domHtml) return;
552+
if (typeof options == "undefined" && typeof eleOrObj == "object") {
553+
options = eleOrObj;
554+
}
532555
options = options || {};
533556

534557
// 'options' that 'Mobilebone.transition()' needs
@@ -584,7 +607,7 @@
584607
}
585608

586609
// excute inline JavaScript
587-
if (Mobilebone.evalScript == true) {
610+
if (Mobilebone.evalScript == true && domHtml.firstintoBind != true) {
588611
slice.call(create.getElementsByTagName("script")).forEach(function(originScript) {
589612
var scriptContent = originScript.innerHTML.trim(), type = originScript.getAttribute("type");
590613
if (scriptContent.trim() == "" || originScript.src) return;
@@ -595,9 +618,10 @@
595618
setTimeout(function() {
596619
head.insertBefore(script, head.firstChild);
597620
head.removeChild(script);
598-
}, 16);
621+
script = null;
622+
}, 17);
599623
originScript = null;
600-
});
624+
});
601625
}
602626

603627
var create_title = create.getElementsByTagName("title")[0];
@@ -639,6 +663,7 @@
639663
optionsTransition.history = false;
640664
optionsTransition.classPage = classPage;
641665
}
666+
642667
// do transition
643668
this.transition(create_page, current_page, optionsTransition);
644669
};
@@ -1195,7 +1220,7 @@
11951220
} else {
11961221
page_in = store[hash];
11971222

1198-
if (isSimple.test(hash) == false) {
1223+
if (page_in && isSimple.test(hash) == false) {
11991224
// ajax store
12001225
Mobilebone.createPage(page_in, {
12011226
url: hash,

test/ajax-html/index.html

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,12 @@
7272
location.href = this.href;
7373
});
7474
};
75-
var page_root_first = function() {
76-
// console.log("root?");
75+
var page_root_first = function(pageinto) {
76+
console.log("firstinto excuted!");
77+
pageinto.querySelector("#ajaxFirstInto").addEventListener("click", function() {
78+
console.log("should console only once!");
79+
this.style.color = "#" + (Math.random() + "").slice(-6) ;
80+
});
7781
};
7882

7983
Mobilebone.evalScript = true;

test/ajax-html/root-reload.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,21 @@
77
<body>
88
<div class="page out" data-callback="root_reload" data-onpagefirstinto="page_root_first">
99
<p>document.getElementById("idShow").innerHTML是:<strong id="idShow">&nbsp;</strong>,应该结果是<strong id="idShould"><?php echo $_GET['id']; ?></strong>.</p>
10+
<p><button id="ajaxFirstInto">pagefirstinto与Ajax载入测试</button></p>
1011
<ul>
11-
<li><a href="#pageHome" id="scriptTest" data-rel="back" data-ajax="true">返回</a></li>
12+
<li><a href="#pageHome" id="scriptTest<?php echo $_GET['id']; ?>" data-rel="back" data-ajax="true">返回</a></li>
1213
<li><a href="root-reload.php?id=8" data-ajax="true">id不一样的页面</a></li>
1314
</ul>
1415
<script>
15-
document.getElementById("scriptTest").style.backgroundColor = "#" + (Math.random() + "").slice(-6);
16-
document.getElementById("idShow").parentNode.addEventListener("touchstart", function() {
17-
document.getElementById("scriptTest").style.backgroundColor = "#" + (Math.random() + "").slice(-6);
18-
});
19-
console.log("script excuted!");
16+
document.getElementById("scriptTest<?php echo $_GET['id']; ?>").style.backgroundColor = "#" + (Math.random() + "").slice(-6);
17+
setTimeout(function() {
18+
19+
document.getElementById("idShow").parentNode.addEventListener("touchstart", function() {
20+
console.log("inline script should once!");
21+
document.getElementById("scriptTest").style.backgroundColor = "#" + (Math.random() + "").slice(-6);
22+
});
23+
}, 350);
24+
console.log("<?php echo $_GET['id']; ?>: script excuted!");
2025
</script>
2126
<script type="text/template">
2227
console.log("should not excute!");

0 commit comments

Comments
 (0)