From 91f75ae66c4d129f6f69e53bd547594e9661f5d5 Mon Sep 17 00:00:00 2001 From: Nate Abele Date: Thu, 17 Apr 2014 14:38:33 -0400 Subject: [PATCH] fix($urlMatcherFactory): detect injected functions Correctly detects injectable functions annotated as arrays when registering new type definitions. --- src/urlMatcherFactory.js | 6 +++++- test/urlMatcherFactorySpec.js | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/urlMatcherFactory.js b/src/urlMatcherFactory.js index f1ed0986c..04a21ea85 100644 --- a/src/urlMatcherFactory.js +++ b/src/urlMatcherFactory.js @@ -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; }); } diff --git a/test/urlMatcherFactorySpec.js b/test/urlMatcherFactorySpec.js index 5844a76ad..433157bcf 100644 --- a/test/urlMatcherFactorySpec.js +++ b/test/urlMatcherFactorySpec.js @@ -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 });