Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

* Promises.js: delayed asset support

  • Loading branch information...
commit 5f6a21af4949bb5eb936c96e25e43271d497a77a 1 parent 5a36ee9
@swannodette swannodette authored
View
18 Promises.js
@@ -66,6 +66,8 @@ var Promise = new Class({
// if handed a Request object initialize it
if(value && value.xhr) {
this.initReq(value);
+ } else if(value && value.name == "DelayedAsset") {
+ this.initDelayedAsset(value);
} else if(value && $type(value) == "array") {
// if handed an array look for promises and watch them.
// watch is not lazy, triggers realize, might want to change this
@@ -132,8 +134,8 @@ var Promise = new Class({
initReq: function(req) {
this.__req = req;
req.addEvent('onSuccess', function(responseText) {
- var json = (!req.options.bare) ? JSON.decode(responseText) : responseText;
- var v = (json.data !== null && json.data !== undefined) ? json.data : json;
+ var json = (!req.options.bare) ? JSON.decode(responseText) : responseText,
+ v = (json.data !== null && json.data !== undefined) ? json.data : json;
this.setValue(this.applyOps(v));
}.bind(this));
req.addEvent('onFailure', function(responseText) {
@@ -141,6 +143,15 @@ var Promise = new Class({
this.fireEvent('error', this);
}.bind(this));
},
+
+
+ initDelayedAsset: function(asset) {
+ this.__asset = asset;
+ asset.addEvent('onload', function() {
+ this.setValue(true);
+ }.bind(this));
+ },
+
/*
Function: setAsync
@@ -172,6 +183,9 @@ var Promise = new Class({
this.__realizing = true;
if(Promise.debug || !this.isAsync()) this.__req.options.async = false;
this.__req.send();
+ } else if(this.__asset && !this.__realizing) {
+ this.__realizing = true;
+ this.__asset.load();
} else if(this.__plain) {
this.setValue(this.value());
} else if(this.__promise) {
View
4 examples/data/test.js
@@ -0,0 +1,4 @@
+var hello = new Element("div", {
+ text: "Hello! I was created by delayed javascript asset!"
+});
+document.body.grab(hello);
View
1  examples/exampleA.html
@@ -6,6 +6,7 @@
<script src="../externals/mootools-1.2.3-core.js" type="text/javascript" charset="utf-8"></script>
<script src="../externals/mootools-1.2.3.1-more.js" type="text/javascript" charset="utf-8"></script>
<script src="../externals/functools/FuncTools.js" type="text/javascript" charset="utf-8"></script>
+ <script src="../externals/DelayedAsset" type="text/javascript" charset="utf-8"></script>
<script src="../Promises.js" type="text/javascript" charset="utf-8"></script>
<script src="exampleA.js" type="text/javascript" charset="utf-8"></script>
<title>Example A</title>
View
7 examples/exampleA.js
@@ -165,6 +165,12 @@ function example11()
show(bar, 'ex11');
}
+function example12()
+{
+ var da = new Promise(new DelayedAsset('javascript', 'data/test.js'));
+ show(da);
+}
+
// Run Examples
// ============
function runExamples()
@@ -186,6 +192,7 @@ function runExamples()
example9();
example10();
example11();
+ example12();
console.log(">>>>>>>>>>>>>>>>>>>> EXIT runExamples");
}
View
42 externals/DelayedAsset.js
@@ -0,0 +1,42 @@
+var DelayedAsset = new Class({
+ Implements: Events,
+ name: "DelayedAsset",
+
+ initialize: function(type, source, properties) {
+ this.type = type;
+ this.source = source;
+ this.properties = properties || {};
+ },
+
+ load: function() {
+ switch(this.type) {
+ case 'javascript':
+ this.asset = new Asset.javascript(this.source, $merge(this.properties, {
+ onload: function() {
+ if($callable(this.properties.onload)) this.properties.onload(this.asset);
+ this.fireEvent('onload', this.asset);
+ }.bind(this)
+ }));
+ break;
+ case 'css':
+ this.asset = new Asset.css(this.source, $merge(this.properties));
+ break;
+ case 'image':
+ this.asset = new Asset.image(this.source, $merge(this.properties, {
+ onload: function() {
+ if($callable(this.properties.onload)) this.properties.onload(this.asset);
+ this.fireEvent('onload', this.asset);
+ }.bind(this),
+ onabort: function() {
+ if($callable(this.properties.onabort)) this.properties.onabort(this.asset);
+ this.fireEvent('onabort', this.asset);
+ }.bind(this),
+ onerror: function() {
+ if($callable(this.properties.onerror)) this.properties.onerror(this.asset);
+ this.fireEvent('onerror', this.asset);
+ }.bind(this)
+ }));
+ break;
+ }
+ }
+});
Please sign in to comment.
Something went wrong with that request. Please try again.