@@ -2207,6 +2207,10 @@ Function.prototype.bind = Function.prototype.bind || function(to) {
2207
2207
trigger ( webview , 'pagebeforeshow' , false ) ;
2208
2208
} ) ;
2209
2209
return webview ;
2210
+ } else {
2211
+ if ( ! url ) {
2212
+ throw new Error ( 'webview[' + id + '] does not exist' ) ;
2213
+ }
2210
2214
}
2211
2215
}
2212
2216
//显示waiting
@@ -2224,7 +2228,7 @@ Function.prototype.bind = Function.prototype.bind || function(to) {
2224
2228
//显示
2225
2229
nShow = $ . showOptions ( options . show ) ;
2226
2230
if ( nShow . autoShow ) {
2227
- webview . addEventListener ( "loaded" , function ( ) {
2231
+ var showWebview = function ( ) {
2228
2232
//关闭等待框
2229
2233
if ( nWaiting ) {
2230
2234
nWaiting . close ( ) ;
@@ -2236,7 +2240,12 @@ Function.prototype.bind = Function.prototype.bind || function(to) {
2236
2240
} ) ;
2237
2241
webview . showed = true ;
2238
2242
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
+ }
2240
2249
}
2241
2250
}
2242
2251
return webview ;
@@ -2270,11 +2279,16 @@ Function.prototype.bind = Function.prototype.bind || function(to) {
2270
2279
} , options . extras ) ) ;
2271
2280
if ( options . subpages ) {
2272
2281
$ . 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
+ }
2278
2292
} ) ;
2279
2293
}
2280
2294
}
@@ -2311,7 +2325,11 @@ Function.prototype.bind = Function.prototype.bind || function(to) {
2311
2325
webview = plus . webview . create ( options . url , id , $ . windowOptions ( options . styles ) , options . extras ) ;
2312
2326
if ( options . subpages ) {
2313
2327
$ . 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
+ }
2315
2333
webview . append ( subWebview ) ;
2316
2334
} ) ;
2317
2335
}
@@ -2384,7 +2402,9 @@ Function.prototype.bind = Function.prototype.bind || function(to) {
2384
2402
var webview ;
2385
2403
if ( ! $ . webviews [ id ] ) { //保证执行一遍
2386
2404
//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
+ }
2388
2408
//之前的实现方案:子窗口loaded之后再append到父窗口中;
2389
2409
//问题:部分子窗口loaded事件发生较晚,此时执行父窗口的children方法会返回空,导致父子通讯失败;
2390
2410
// 比如父页面执行完preload事件后,需触发子页面的preload事件,此时未append的话,就无法触发;
@@ -2394,6 +2414,7 @@ Function.prototype.bind = Function.prototype.bind || function(to) {
2394
2414
plus . webview . currentWebview ( ) . append ( webview ) ;
2395
2415
// });
2396
2416
$ . webviews [ id ] = options ;
2417
+
2397
2418
}
2398
2419
return webview ;
2399
2420
} ;
0 commit comments