Permalink
Browse files

* Promises.js: added 11th example. Promises can hold promises, not ju…

…st requests. Fixed Promise.get.
  • Loading branch information...
swannodette committed Dec 25, 2009
1 parent e921746 commit 5a36ee94d1eabb23c6fd3383b385866d8ee42f66
Showing with 32 additions and 6 deletions.
  1. +23 −5 Promises.js
  2. +9 −1 examples/exampleA.js
View
@@ -85,6 +85,7 @@ var Promise = new Class({
}.bind(this));
} else if(Promise.isPromise(value)) {
// if handed a promise, watch it
+ this.__promise = value;
value.addEvent('realized', function() {
this.setValue(value.value());
}.bind(this));
@@ -173,6 +174,15 @@ var Promise = new Class({
this.__req.send();
} else if(this.__plain) {
this.setValue(this.value());
+ } else if(this.__promise) {
+ if(this.__promise.isRealized())
+ {
+ this.setValue(this.__promise.value());
+ }
+ else
+ {
+ this.__promise.realize();
+ }
}
return this;
},
@@ -230,12 +240,12 @@ var Promise = new Class({
setValue: function(value, notify) {
if(value && value.xhr) {
this.initReq(value);
+ } else if(!this.__realized && notify !== false) {
+ this.__realized = true;
+ this.__value = this.applyOps(value);
+ this.fireEvent('realized', this.__value);
} else {
this.__value = value;
- if(!this.__realized && notify !== false) {
- this.__realized = true;
- this.fireEvent('realized', this.applyOps(this.__value));
- }
}
},
@@ -310,7 +320,15 @@ var Promise = new Class({
*/
get: function() {
var args = $A(arguments);
- if(!this.isRealized()) return (new Promise(this, {lazy:this.options.lazy})).op(function(v) { return $get.apply(null, [v].extend(args)); });
+ if(!this.isRealized())
+ {
+ return (new Promise(this, {lazy:this.options.lazy})).op(
+ function(v) {
+ var result = $get.apply(null, [v].extend(args));
+ return result;
+ }
+ );
+ }
return Function.get.apply(null, [this.value()].extend(args));
},
View
@@ -31,7 +31,7 @@ var show = function show(value, target)
}
else
{
- console.log("show: " + value);
+ console.log("show:", value);
}
}.decorate(promise);
@@ -158,6 +158,13 @@ function example10()
p2.realize();
}
+function example11()
+{
+ var nested = get("nested"),
+ bar = nested.get("foo", "bar");
+ show(bar, 'ex11');
+}
+
// Run Examples
// ============
function runExamples()
@@ -178,6 +185,7 @@ function runExamples()
example8();
example9();
example10();
+ example11();
console.log(">>>>>>>>>>>>>>>>>>>> EXIT runExamples");
}

0 comments on commit 5a36ee9

Please sign in to comment.