Skip to content

Commit 70c6659

Browse files
committed
feat(resolve): add $resolve service
- Add `$resolve` service for exposing helper as an injectable
1 parent 2aad08d commit 70c6659

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/common/common.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,7 @@ export function tail(collection: any[]): any {
411411
* in your angular app (use {@link ui.router} module instead).
412412
*
413413
*/
414-
angular.module('ui.router.util', ['ng', 'ui.router.init']);
414+
angular.module('ui.router.util', ['ng', 'ui.router.init', 'ui.router.resolve']);
415415

416416
/**
417417
* @ngdoc overview

src/ng1/angular1.ts

+27-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,12 @@
1414
import {IQService} from "angular";
1515
import {Router} from "../router";
1616
import {services} from "../common/coreservices";
17-
import {isObject} from "../common/common";
17+
import {forEach, isObject} from "../common/common";
18+
import {RawParams} from "../params/interface";
19+
import {Node} from "../path/module";
20+
import {Resolvables} from "../resolve/interface";
21+
import {Resolvable, ResolveContext} from "../resolve/module";
22+
import {State} from "../state/module";
1823

1924
let app = angular.module("ui.router.angular1", []);
2025

@@ -80,21 +85,40 @@ function ng1UIRouter($locationProvider) {
8085
};
8186

8287
bindFunctions(["replace", "url", "path", "search", "hash"], $location, services.location);
83-
bindFunctions([ 'port', 'protocol', 'host'], $location, services.locationConfig);
88+
bindFunctions(['port', 'protocol', 'host'], $location, services.locationConfig);
8489
bindFunctions(['baseHref'], $browser, services.locationConfig);
8590

8691
return router;
8792
}
8893
}
8994

95+
function resolveFactory() {
96+
return {
97+
resolve: (invocables, locals, parent, self) => {
98+
let state = new State({ params: {} });
99+
let node = new Node(state, <RawParams> {});
100+
let context = new ResolveContext([node]);
101+
let resolvables: Resolvables = {};
102+
forEach(invocables, (invocable, key) => {
103+
resolvables[key] = new Resolvable(`${key}`, invocable);
104+
});
105+
106+
context.addResolvables(resolvables, node.state);
107+
108+
return context.resolvePath();
109+
}
110+
};
111+
}
112+
90113
angular.module('ui.router.init', []).provider("ng1UIRouter", <any> ng1UIRouter);
91114
// Register as a provider so it's available to other providers
92115
angular.module('ui.router.util').provider('$urlMatcherFactory', ['ng1UIRouterProvider', () => router.urlMatcherFactory]);
93116
angular.module('ui.router.router').provider('$urlRouter', ['ng1UIRouterProvider', () => router.urlRouterProvider]);
94117
angular.module('ui.router.state').provider('$state', ['ng1UIRouterProvider', () => router.stateProvider]);
118+
angular.module('ui.router.resolve', []).factory('$resolve', <any> resolveFactory);
95119

96120
/* This effectively calls $get() to init when we enter runtime */
97121
angular.module('ui.router.init').run(['ng1UIRouter', function(ng1UIRouter) { }]);
122+
angular.module('ui.router.resolve').run(['$resolve', function(resolve) { }]);
98123
angular.module('ui.router.state').run(['$state', function($state) { }]);
99124
angular.module('ui.router.util').run(['$urlMatcherFactory', function($urlMatcherFactory) { }]);
100-

0 commit comments

Comments
 (0)