2010-10-08 23:43:40 +00:00
|
|
|
/**
|
|
|
|
|
* A future action in a spec.
|
|
|
|
|
*/
|
|
|
|
|
angular.scenario.Future = function(name, behavior) {
|
2010-06-29 22:43:02 +00:00
|
|
|
this.name = name;
|
|
|
|
|
this.behavior = behavior;
|
|
|
|
|
this.fulfilled = false;
|
2010-10-08 23:43:40 +00:00
|
|
|
this.value = undefined;
|
2010-10-18 21:02:18 +00:00
|
|
|
this.parser = angular.identity;
|
2010-10-08 23:43:40 +00:00
|
|
|
};
|
2010-06-29 22:43:02 +00:00
|
|
|
|
2010-10-08 23:43:40 +00:00
|
|
|
/**
|
|
|
|
|
* Executes the behavior of the closure.
|
|
|
|
|
*
|
|
|
|
|
* @param {Function} Callback function(error, result)
|
|
|
|
|
*/
|
|
|
|
|
angular.scenario.Future.prototype.execute = function(doneFn) {
|
|
|
|
|
this.behavior(angular.bind(this, function(error, result) {
|
2010-06-29 22:43:02 +00:00
|
|
|
this.fulfilled = true;
|
2010-10-18 21:02:18 +00:00
|
|
|
if (result) {
|
|
|
|
|
try {
|
|
|
|
|
result = this.parser(result);
|
|
|
|
|
} catch(e) {
|
|
|
|
|
error = e;
|
|
|
|
|
}
|
|
|
|
|
}
|
2010-10-08 23:43:40 +00:00
|
|
|
this.value = error || result;
|
|
|
|
|
doneFn(error, result);
|
|
|
|
|
}));
|
2010-06-29 22:43:02 +00:00
|
|
|
};
|
2010-10-18 21:02:18 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Configures the future to convert it's final with a function fn(value)
|
|
|
|
|
*/
|
|
|
|
|
angular.scenario.Future.prototype.parsedWith = function(fn) {
|
|
|
|
|
this.parser = fn;
|
|
|
|
|
return this;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Configures the future to parse it's final value from JSON
|
|
|
|
|
* into objects.
|
|
|
|
|
*/
|
|
|
|
|
angular.scenario.Future.prototype.fromJson = function() {
|
|
|
|
|
return this.parsedWith(angular.fromJson);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Configures the future to convert it's final value from objects
|
|
|
|
|
* into JSON.
|
|
|
|
|
*/
|
|
|
|
|
angular.scenario.Future.prototype.toJson = function() {
|
|
|
|
|
return this.parsedWith(angular.toJson);
|
|
|
|
|
};
|