From 1e0a79c56eb0c2b57f1cad66dc0619f010225faa Mon Sep 17 00:00:00 2001 From: Steve Orvell Date: Wed, 18 Dec 2013 15:04:09 -0800 Subject: [PATCH] Refine timing related to calling Polymer() in main document scripts when polymer is imported. --- src/dom.js | 42 +++++++++++++++++++++++++++++------------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/src/dom.js b/src/dom.js index 162c949..f3f6521 100644 --- a/src/dom.js +++ b/src/dom.js @@ -117,23 +117,39 @@ } return dom; } + // Make a stub for Polymer() for polyfill purposes; under the HTMLImports + // polyfill, scripts in the main document run before imports. That means + // if (1) polymer is imported and (2) Polymer() is called in the main document + // in a script after the import, 2 occurs before 1. We correct this here + // by specfiically patching Polymer(); this is not necessary under native + // HTMLImports. + var elementDeclarations = []; + + var polymerStub = function(name, dictionary) { + elementDeclarations.push(arguments); + } + window.Polymer = polymerStub; - var polymer = function(name, dictionary) { - document.addEventListener('WebComponentsReady', function() { - // avoid re-entrancy. If polymer is not redefined by this time, do nothing - if (window.Polymer !== polymer) { - Polymer(name, dictionary); - } else { - console.warn('You tried to use polymer without loading it first. To ' + - 'load polymer, '); - } - }, true); + // deliver queued delcarations + scope.deliverDeclarations = function() { + scope.deliverDeclarations = null; + return elementDeclarations; } - window.Polymer = polymer - // exports + // Once DOMContent has loaded, any main document scripts that depend on + // Polymer() should have run. Calling Polymer() now is an error until + // polymer is imported. + window.addEventListener('DOMContentLoaded', function() { + if (window.Polymer === polymerStub) { + window.Polymer = function() { + console.error('You tried to use polymer without loading it first. To ' + + 'load polymer, '); + }; + } + }); + // exports scope.createDOM = createDOM; })(window.Platform);