Skip to content

Commit

Permalink
fix(resolve): Fix regression; Allow resolve values to be service names
Browse files Browse the repository at this point in the history
closes #2588
  • Loading branch information
christopherthielen committed Apr 12, 2016
1 parent adfbde3 commit a34fd3b
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/ng1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from "./core";

export * from "./ng1/services";
export * from "./ng1/viewsBuilder";
export * from "./ng1/resolvesBuilder";

import "./ng1/stateDirectives";
import "./ng1/stateFilters";
Expand Down
18 changes: 18 additions & 0 deletions src/ng1/resolvesBuilder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/** @module ng1 */ /** */
import {State} from "../state/stateObject";
import {forEach} from "../common/common";
import {isString} from "../common/predicates";

/**
* This is a [[StateBuilder.builder]] function for angular1 `resolve:` block on a [[Ng1StateDeclaration]].
*
* When the [[StateBuilder]] builds a [[State]] object from a raw [[StateDeclaration]], this builder
* handles the `resolve` property with logic specific to angular-ui-router (ng1).
*/
export function ng1ResolveBuilder(state: State) {
let resolve = {};
forEach(state.resolve || {}, function (resolveFn, name: string) {
resolve[name] = isString(resolveFn) ? [ resolveFn, x => x ] : resolveFn;
});
return resolve;
}
2 changes: 2 additions & 0 deletions src/ng1/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {State} from "../state/module";
import {trace} from "../common/trace";
import {ng1ViewsBuilder, ng1ViewConfigFactory, Ng1ViewConfig} from "./viewsBuilder";
import {TemplateFactory} from "./templateFactory";
import {ng1ResolveBuilder} from "./resolvesBuilder";

/** @hidden */
let app = angular.module("ui.router.angular1", []);
Expand Down Expand Up @@ -160,6 +161,7 @@ function ng1UIRouter($locationProvider) {

// Apply ng1 `views` builder to the StateBuilder
router.stateRegistry.decorator("views", ng1ViewsBuilder);
router.stateRegistry.decorator("resolve", ng1ResolveBuilder);

router.viewService.viewConfigFactory('ng1', ng1ViewConfigFactory);

Expand Down
11 changes: 10 additions & 1 deletion test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ describe('state helpers', function() {
matcher = new StateMatcher(states);
builder = new StateBuilder(matcher, urlMatcherFactoryProvider);
builder.builder('views', uiRouter.ng1ViewsBuilder);
builder.builder('resolve', uiRouter.ng1ResolveBuilder);
});

describe('interface', function() {
Expand Down Expand Up @@ -177,7 +178,7 @@ describe('state helpers', function() {
expect(urlMatcherFactoryProvider.isMatcher).toHaveBeenCalledWith({ foo: "bar" });
});

it('should return a new views object, and copy keys, if no `views` is defined in the state def', function() {
it('should return a new views object, and copy keys from state def, if no `views` is defined in the state def', function() {
var parent = { name: "" };
var config = { url: "/foo", templateUrl: "/foo.html", controller: "FooController", parent: parent };
var built = builder.builder('views')(config);
Expand All @@ -191,6 +192,14 @@ describe('state helpers', function() {
var config = { a: { foo: "bar", controller: "FooController" } };
expect(builder.builder('views')({ parent: parent, views: config })).toEqual(config);
});

it("should replace a resolve: string value with a function that injects the service of the same name", inject(function($injector) {
var config = { resolve: { foo: "bar" } };
var locals = { "bar": 123 };
expect(builder.builder('resolve')).toBeDefined();
var built = builder.builder('resolve')(config);
expect($injector.invoke(built.foo, null, locals)).toBe(123);
}));
});
});

Expand Down

0 comments on commit a34fd3b

Please sign in to comment.