angular.js/src/scenario/Future.js

55 lines
1.2 KiB
JavaScript
Raw Normal View History

/**
* 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;
this.value = undefined;
this.parser = angular.identity;
};
2010-06-29 22:43:02 +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;
if (result) {
try {
result = this.parser(result);
} catch(e) {
error = e;
}
}
this.value = error || result;
doneFn(error, result);
}));
2010-06-29 22:43:02 +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);
};