Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Refine timing related to calling Polymer() in main document scripts w…
Browse files Browse the repository at this point in the history
…hen polymer is imported.
  • Loading branch information
sorvell committed Dec 18, 2013
1 parent 9edd9e2 commit 1e0a79c
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -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, <link rel="import" href="' +
'components/polymer/polymer.html">');
}
}, 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, <link rel="import" href="' +
'components/polymer/polymer.html">');
};
}
});

// exports
scope.createDOM = createDOM;

})(window.Platform);

0 comments on commit 1e0a79c

Please sign in to comment.