angular.js/src/scenario/Future.js
Elliott Sprehn e7e894a2e3 Significantly clean up the way the scenario DSL works and implement many more DSL statements.
- "this" always means the current chain scope inside a DSL

- addFutureAction callbacks now take ($window, $document, done)

- $document has a special method elements() that uses the currently selected nodes in the document as defined by using() statements.

- $document.elements() allows placeholder insertion into selectors to make them more readable.
  ex. $document.elements('input[name="$1"]', myVar) will substitute the value of myVar for $1 in the selector. Subsequent arguments are $2 and so on.

- $document.elements() results have a special method trigger(event) which should be used to events. This method implements some hacks to make sure browser UI controls update and the correct angular events fire.

- futures now allow custom formatting. By default any chain that results in a future can use toJson() or fromJson() to convert the future value to and from json. A custom parser can be provided with parsedWith(fn) where fn is a callback(value) that must return the parsed result.

Note: The entire widgets.html UI is now able to be controlled and asserted through DSL statements!!! Victory! :)
2010-10-19 00:45:38 -07:00

54 lines
1.2 KiB
JavaScript

/**
* A future action in a spec.
*/
angular.scenario.Future = function(name, behavior) {
this.name = name;
this.behavior = behavior;
this.fulfilled = false;
this.value = undefined;
this.parser = angular.identity;
};
/**
* 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) {
this.fulfilled = true;
if (result) {
try {
result = this.parser(result);
} catch(e) {
error = e;
}
}
this.value = error || result;
doneFn(error, result);
}));
};
/**
* 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);
};