Skip to content

Commit 4d349ae

Browse files
committed
Fix:subpage重复创建的bug
1 parent 1bb4263 commit 4d349ae

File tree

5 files changed

+7647
-26
lines changed

5 files changed

+7647
-26
lines changed

dist/js/mui.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2207,6 +2207,10 @@ Function.prototype.bind = Function.prototype.bind || function(to) {
22072207
trigger(webview, 'pagebeforeshow', false);
22082208
});
22092209
return webview;
2210+
} else {
2211+
if (!url) {
2212+
throw new Error('webview[' + id + '] does not exist');
2213+
}
22102214
}
22112215
}
22122216
//显示waiting
@@ -2224,7 +2228,7 @@ Function.prototype.bind = Function.prototype.bind || function(to) {
22242228
//显示
22252229
nShow = $.showOptions(options.show);
22262230
if (nShow.autoShow) {
2227-
webview.addEventListener("loaded", function() {
2231+
var showWebview = function() {
22282232
//关闭等待框
22292233
if (nWaiting) {
22302234
nWaiting.close();
@@ -2236,7 +2240,12 @@ Function.prototype.bind = Function.prototype.bind || function(to) {
22362240
});
22372241
webview.showed = true;
22382242
options.afterShowMethodName && webview.evalJS(options.afterShowMethodName + '(\'' + JSON.stringify(params) + '\')');
2239-
}, false);
2243+
};
2244+
if (!url) {
2245+
showWebview();
2246+
} else {
2247+
webview.addEventListener("loaded", showWebview, false);
2248+
}
22402249
}
22412250
}
22422251
return webview;
@@ -2270,11 +2279,16 @@ Function.prototype.bind = Function.prototype.bind || function(to) {
22702279
}, options.extras));
22712280
if (options.subpages) {
22722281
$.each(options.subpages, function(index, subpage) {
2273-
//TODO 子窗口也可能已经创建,比如公用模板的情况;
2274-
var subWebview = plus.webview.create(subpage.url, subpage.id || subpage.url, $.windowOptions(subpage.styles), $.extend({
2275-
preload: true
2276-
}, subpage.extras));
2277-
webview.append(subWebview);
2282+
var subpageId = subpage.id || subpage.url;
2283+
if (subpageId) { //过滤空对象
2284+
var subWebview = plus.webview.getWebviewById(subpageId);
2285+
if (!subWebview) { //如果该webview不存在,则创建
2286+
subWebview = plus.webview.create(subpage.url, subpageId, $.windowOptions(subpage.styles), $.extend({
2287+
preload: true
2288+
}, subpage.extras));
2289+
}
2290+
webview.append(subWebview);
2291+
}
22782292
});
22792293
}
22802294
}
@@ -2311,7 +2325,11 @@ Function.prototype.bind = Function.prototype.bind || function(to) {
23112325
webview = plus.webview.create(options.url, id, $.windowOptions(options.styles), options.extras);
23122326
if (options.subpages) {
23132327
$.each(options.subpages, function(index, subpage) {
2314-
var subWebview = plus.webview.create(subpage.url, subpage.id || subpage.url, $.windowOptions(subpage.styles), subpage.extras);
2328+
var subpageId = subpage.id || subpage.url;
2329+
var subWebview = plus.webview.getWebviewById(subpageId);
2330+
if (!subWebview) {
2331+
subWebview = plus.webview.create(subpage.url, subpageId, $.windowOptions(subpage.styles), subpage.extras);
2332+
}
23152333
webview.append(subWebview);
23162334
});
23172335
}
@@ -2384,7 +2402,9 @@ Function.prototype.bind = Function.prototype.bind || function(to) {
23842402
var webview;
23852403
if (!$.webviews[id]) { //保证执行一遍
23862404
//TODO 这里也有隐患,比如某个webview不是作为subpage创建的,而是作为target webview的话;
2387-
webview = plus.webview.create(options.url, id, options.styles, options.extras);
2405+
if (!plus.webview.getWebviewById(id)) {
2406+
webview = plus.webview.create(options.url, id, options.styles, options.extras);
2407+
}
23882408
//之前的实现方案:子窗口loaded之后再append到父窗口中;
23892409
//问题:部分子窗口loaded事件发生较晚,此时执行父窗口的children方法会返回空,导致父子通讯失败;
23902410
// 比如父页面执行完preload事件后,需触发子页面的preload事件,此时未append的话,就无法触发;
@@ -2394,6 +2414,7 @@ Function.prototype.bind = Function.prototype.bind || function(to) {
23942414
plus.webview.currentWebview().append(webview);
23952415
// });
23962416
$.webviews[id] = options;
2417+
23972418
}
23982419
return webview;
23992420
};

dist/js/mui.min.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)