Skip to content

Commit

Permalink
fix($urlMatcherFactory): detect injected functions
Browse files Browse the repository at this point in the history
Correctly detects injectable functions annotated as arrays when registering new type definitions.
  • Loading branch information
nateabele committed Apr 17, 2014
1 parent 67be0bd commit 91f75ae
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/urlMatcherFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -675,12 +675,16 @@ function $UrlMatcherFactory() {
return this;
}];

// To ensure proper order of operations in object configuration, and to allow internal
// types to be overridden, `flushTypeQueue()` waits until `$urlMatcherFactory` is injected
// before actually wiring up and assigning type definitions
function flushTypeQueue() {
forEach(typeQueue, function(type) {
if (UrlMatcher.prototype.$types[type.name]) {
throw new Error("A type named '" + type.name + "' has already been defined.");
}
var def = new Type(isFunction(type.def) ? injector.invoke(type.def) : type.def);
var isAnnotated = isFunction(type.def) || isArray(type.def);
var def = new Type(isAnnotated ? injector.invoke(type.def) : type.def);
UrlMatcher.prototype.$types[type.name] = def;
});
}
Expand Down
11 changes: 11 additions & 0 deletions test/urlMatcherFactorySpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,17 @@ describe("urlMatcherFactory", function () {
expect($umf.type("myType").decode()).toBe($stateParams);
}));

it("should accept annotated function definitions", inject(function ($stateParams) {
$umf.type("myAnnotatedType", ['$stateParams', function(s) {
return {
decode: function() {
return s;
}
};
}]);
expect($umf.type("myAnnotatedType").decode()).toBe($stateParams);
}));

it("should match built-in types", function () {
var m = new UrlMatcher("/{foo:int}/{flag:bool}");
expect(m.exec("/1138/1")).toEqual({ foo: 1138, flag: true });
Expand Down

0 comments on commit 91f75ae

Please sign in to comment.