Permalink
Browse files

Moving to a more robust Javascript require mechanism.

  • Loading branch information...
pvande committed Jan 27, 2012
1 parent 7126324 commit 15c1cec90a8d1b5b4c5e3619b983054c55127835
Showing with 21 additions and 38 deletions.
  1. +19 −25 public/javascript/application.js
  2. +2 −13 views/index.erb
@@ -22,21 +22,21 @@ var Tranquil = {
},
};
-function requireJavascript(script, callback) {
- var tag = document.querySelectorAll('script[src="' + script + '"]')[0];
- if (tag) {
- var fn = tag.onload;
- tag.onload = function () { fn && fn(); return callback() };
- } else {
- tag = document.createElement('script');
- tag.src = script;
- tag.type = 'text/javascript';
- tag.charset = 'utf-8';
- tag.onload = callback;
- document.head.appendChild(tag);
- return true;
- }
- return false;
+// From: https://gist.github.com/1680738
+requireJavascript = function R(s,c,d,t){d=document,R[s]=t=R[s]||d.head.appendChild(d.createElement('script')),d=t.onload;t.x?c():t.onload=function(){c(t.x=1,d&&d())},t.src=s};
+
+function requireAllJavascript(scripts, callback) {
+ scripts = (scripts || []).slice(0);
+ if (!scripts.length) return callback();
+
+ scripts.forEach(function(script) {
+ requireJavascript(script, function() {
+ scripts = scripts.filter(function(s) {
+ return s !== script;
+ });
+ if (!scripts.length) callback();
+ });
+ });
}
function requireStylesheet(style) {
@@ -78,16 +78,10 @@ function buildLayout(layout) {
var type = Tranquil[cell.type];
if (!type)
return div.innerHTML = "Could not load type '" + cell.type + "'!";
- if (type.javascript && !type.javascript.loaded) {
- type.javascript.loaded = 0;
- return type.javascript.forEach(function(e) {
- requireJavascript(e, function() {
- type.javascript.loaded += 1;
- if (type.javascript.loaded == type.javascript.length) setTimeout(fn, 300);
- });
- });
- }
- Tranquil[cell.type].call(div, cell);
+
+ requireAllJavascript(type.javascript, function() {
+ Tranquil[cell.type].call(div, cell);
+ }, cell.type);
};
if (!(cell.type in Tranquil)) {
requireJavascript('/javascript/' + cell.type + '.js', fn);
View
@@ -25,22 +25,11 @@
}
});
- var doLayout = function() {
- func.lastElements = buildLayout(config.layout);
- };
-
(config.stylesheet || []).forEach(requireStylesheet);
- var scripts = (config.javascript || []);
- scripts.loaded = 0;
- scripts.forEach(function(e) {
- requireJavascript(e, function() {
- scripts.loaded += 1;
- if (scripts.loaded == scripts.length) setTimeout(doLayout, 300);
- });
+ requireAllJavascript(config.javascript, function() {
+ func.lastElements = buildLayout(config.layout);
});
-
- scripts.length || doLayout();
}
setTimeout(function() {

0 comments on commit 15c1cec

Please sign in to comment.