-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Implemented promise pattern for async programming
- Onload support for CSS - Refactored template for better async loading using promises
- Loading branch information
1 parent
fc331e6
commit 9a29e59
Showing
10 changed files
with
277 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,58 @@ | ||
define(function () { | ||
var baseTheme = "themes/base/"; | ||
var stylesheets = {}; | ||
return { | ||
load: function (file/*, callback*/) { | ||
if (file.substr(file.length - 4) !== ".css") { | ||
file += ".css"; | ||
} | ||
if (!(file in stylesheets)) { | ||
var link = document.createElement("link"); | ||
link.rel = "stylesheet"; | ||
link.type = "text/css"; | ||
/* Not supported in Gecko/Webkit | ||
link.addEventListener("load", function (event) { | ||
callback(); | ||
link.removeEventListener("load", this); | ||
}, false); | ||
*/ | ||
link.href = baseTheme + file; | ||
stylesheets[file] = link; | ||
document.getElementsByTagName("head")[0].appendChild(link); | ||
define(["core/promise"], function (promise) { | ||
var theme = "themes/base/", | ||
extension = ".css", | ||
stylesheets = {}, | ||
fixName = function (file) { | ||
if (file.substr(file.length - extension.length) !== extension) { | ||
file += extension; | ||
} | ||
return file; | ||
}, | ||
unload: function (file) { | ||
if (file.substr(file.length - 4) !== ".css") { | ||
file += ".css"; | ||
} | ||
load = function (file) { | ||
return new promise(function (deferred) { | ||
if (!(file in stylesheets)) { | ||
var link = document.createElement("link"); | ||
link.rel = "stylesheet"; | ||
/* * / | ||
// Load event on LINK is currently not supported by Gecko/Webkit. | ||
// + https://bugzilla.mozilla.org/show_bug.cgi?id=185236 | ||
// + http://code.google.com/p/chromium/issues/detail?id=67522 | ||
// + https://bugs.webkit.org/show_bug.cgi?id=38995 | ||
link.addEventListener("load", function (event) { | ||
link.removeEventListener("load", this); | ||
deferred.done(); | ||
}, false); | ||
/* */ | ||
link.href = theme + file; | ||
stylesheets[file] = link; | ||
document.getElementsByTagName("head")[0].appendChild(link); | ||
// Load event workaround | ||
console.log("css load", theme + file); | ||
var img = document.createElement('img'); | ||
img.src = theme + file; | ||
img.style.display = "none"; | ||
img.addEventListener("error", function () { | ||
console.log("css done", theme + file); | ||
document.body.removeChild(img); | ||
deferred.done(); | ||
}, false); | ||
document.body.appendChild(img); | ||
} | ||
}); | ||
}, | ||
unload = function (file) { | ||
if (file in stylesheets) { | ||
var link = stylesheets[file]; | ||
link.parentNode.removeChild(link); | ||
delete stylesheets[file]; | ||
} | ||
}; | ||
return { | ||
load: function (file) { | ||
return load(fixName(file)); | ||
}, | ||
unload: function (file) { | ||
return unload(fixName(file)); | ||
} | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
define(function () { | ||
return function (promisor) { | ||
var callbacks = [], | ||
promises = [], | ||
pending = 0, | ||
unique = parseInt(Math.random() * 1000000), | ||
deferred = { | ||
done: function () { | ||
console.log("promise", unique, "done", pending); | ||
var payload = arguments; | ||
pending--; | ||
if (pending <= 0) { | ||
console.log("promise", unique, "FEUER FREI!", pending); | ||
callbacks.forEach(function (callback) { | ||
callback.apply(this, payload); | ||
}); | ||
} | ||
return this; | ||
} | ||
}; | ||
if (promisor instanceof Function) { | ||
setTimeout(function () { promisor(deferred); }, 0); | ||
//promisor(deferred); | ||
} | ||
return { | ||
when: function (promise) { | ||
console.log("promise", unique, "when", pending); | ||
if (promises.indexOf(promise) === -1) { | ||
promises.push(promise); | ||
pending++; | ||
promise.then(deferred.done); | ||
} | ||
return this; | ||
}, | ||
then: function (success /* , failure, progress */ ) { // TODO | ||
console.log("promise", unique, "then", pending); | ||
if (callbacks.indexOf(success) === -1) { | ||
callbacks.push(success); | ||
} | ||
return this; | ||
} | ||
}; | ||
}; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.