2017-02-21 75 views
1

我试图让加载屏幕在聚合物工作,因为在这个要点看到工作:https://gist.github.com/SlicedSilver/f2e93a5995f84d9cd512获取加载scren与聚合物

的想法很简单:入口点是一个轻量级的HTML文件呈现加载屏幕,其主体包含一个onload回调,一旦呈现轻量级页面,该回调将通过DOM加载Polymer应用程序。

它在桌面浏览器上运行得非常漂亮。在移动浏览器上,filestoload.html永远不会被加载(但也不会引发任何错误),因此加载屏幕只停留在那里,应用程序不会加载。完整的代码如下,但特别注意去这条线:

tag.setAttribute('onload', 'polymerLoader.insertPolymerApplication()'); 

这onload事件永远不会触发并不会抛出一个错误。我试图通过DOM和适当的事件处理器来捕获它,但无济于事。我甚至试过把它排除所有的完整性检查的JS手中,加入像这样的链接的HTML文件:

<link rel="import" href="/filestoload.html" onload="polymerLoader.insertPolymerApplication()" > 

同样的结果 - 显示在桌面上的应用程序,但只显示了在手机上加载屏幕。我刚刚从创意中脱颖而出。任何帮助?

....

这里是轻量级的入口点(的index.html)

<!doctype html> 
 
<html lang="en"> 
 
    <head> 
 
    <meta charset="utf-8"> 
 
    <meta name="viewport" content="width=device-width, minimum-scale=1, initial-scale=1, user-scalable=yes"> 
 
    <link rel="shortcut icon" type="image/png" href="/assets/images/favicon.ico"/> 
 

 
    <title>GreenMaven</title> 
 
    <meta name="description" content="greenmaven description"> 
 

 
    <script src="assets/js/polymerAppLoader.js"></script> 
 

 
    <style type="text/css"> 
 

 
     .loading { 
 
     position: fixed; 
 
     top: 50%; 
 
     left: 50%; 
 
     /* bring your own prefixes */ 
 
     transform: translate(-50%, -50%); 
 
     z-index: -500; 
 
     display: block; 
 
     } 
 
    </style> 
 
    </head> 
 
    <body onload="polymerLoader.loadPolymerApplication()"> 
 

 
    <div id="loader" class="loading"> 
 
     <img src="assets/images/gears.svg" /> 
 
    </div> 
 
    
 
    </body> 
 
</html>

而这里的加载聚合物应用程序的其余部分JS:

'use strict'; 
 
/* global polymerLoader */ 
 
/*jshint unused:false*/ 
 
/*jshint -W079*/ 
 

 

 
// This is the normal conditional loader for the Web components Polyfill 
 
if ('registerElement' in document && 'createShadowRoot' in HTMLElement.prototype && 'import' in document.createElement('link') && 'content' in document.createElement('template')) { 
 
    // We're using a browser with native WC support! 
 
} else { 
 
    // Add web components polyfill... 
 
    document.write('<script src="bower_components/webcomponentsjs/webcomponents.lite.js"><\/script>'); 
 
} 
 

 
var polymerLoader = (function() { 
 

 
    // Function for creating a link element and inserting it into the <head> of the html document 
 
    function addLinkTag(elementType, address, shim, loadTrigger) { 
 
    var tag = document.createElement('link'); 
 
    tag.rel = elementType; 
 
    tag.href = address; 
 
    if (shim) { 
 
     // add the shim-shadowdom attribute 
 
     tag.setAttribute('shim-shadowdom', ''); 
 
    } 
 
    if (loadTrigger) { 
 
     // This file needs to be loaded before inserting the Polymer Application 
 
     // when finished loading it will call the polymerLoader.insertPolymerApplication() function 
 
     tag.setAttribute('onload', 'polymerLoader.insertPolymerApplication()'); 
 
     expectedCalls++; 
 
    } 
 
    document.getElementsByTagName('head')[0].appendChild(tag); 
 
    } 
 

 
    var pgApploaded = false; 
 

 
    function loadPolymerApplication() { 
 
    // Only insert once. 
 
    if (!pgApploaded) { 
 
     addLinkTag('import', 'filestoload.html', false, true); 
 
     pgApploaded = true; 
 
    } 
 
    } 
 

 
    // Counter variable for insertPolymerApplication() calls 
 
    var callCount = 0; 
 
    var expectedCalls = 0; 
 

 
    function insertPolymerApplication() { 
 
    callCount++; 
 
    // Only when callCount >= expectedCalls 
 
    // The application is only inserted after all required files have loaded 
 
    // for the application to work. 
 
    if (callCount >= expectedCalls) { 
 
     // here is the html that is inserted when everything is loaded. 
 
     document.querySelector('body').innerHTML += '<template is="auto-binding" id="app"><polymer-app id="main-app"></polymer-app></template>'; 
 
     document.getElementById('loader').style.display = 'none'; 
 
    } 
 
    } 
 

 

 
    return { 
 
    insertPolymerApplication: function() { 
 
     insertPolymerApplication(); 
 
    }, 
 

 
    loadPolymerApplication: function() { 
 
     loadPolymerApplication(); 
 
    } 
 
    }; 
 
})(document);

最后,这里是一个已经将通常在聚合物的index.html中找到的链接和脚本文件filestoload.html:

<!doctype html> 
 
<!-- Here is where you put the scripts required by the page, that would normally be --> 
 
<!-- included in the index.html page, you can still use grunt/gulp build functions on these --> 
 

 
<!-- will be replaced with elements/elements.vulcanized.html --> 
 
<link rel="manifest" href="/manifest.json"> 
 
<link rel="import" href="/src/greenmaven-app/greenmaven-app.html"> 
 
<link rel="stylesheet" href="assets/css/main.css"> 
 
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" integrity="sha384-rwoIResjU2yc3z8GV/NPeZWAv56rSmLldC3R/AZzGRnGxQQKnKkoFVhFQhNUwEyJ" crossorigin="anonymous"> 
 

 
<!-- endreplace--> 
 

 
<!-- build:js scripts/app.js --> 
 
<script src="properties_base/farmhacker-properties.js"></script> 
 
<!-- endbuild--> 
 

 
<!-- build:js scripts/thirdparty.js --> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script> 
 
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script> 
 
    
 
    <script> 
 
     $(function() { 
 
     $('a[href*="#"]:not([href="#"])').click(function() { 
 
      if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) { 
 
      var target = $(this.hash); 
 
      target = target.length ? target : $('[name=' + this.hash.slice(1) +']'); 
 
      if (target.length) { 
 
       $('html, body').animate({ 
 
       scrollTop: target.offset().top - $('#navbar').height() 
 
       }, 1000); 
 
       return false; 
 
      } 
 
      } 
 
     }); 
 
     }); 
 
    </script> 
 
<!-- endbuild-->

回答

0

可谓是Polymer build中的一些错误,可能与运行polymer build导致应用程序抛出一堆来自鲍尔组件的一堆404错误的另一个错误有关。

当我对服务器进行原始部署(没有构建/硫化)时,桌面和移动设备上的一切运行良好,并且没有出现错误。