Skip to content

Commit

Permalink
feat(resolve): add $resolve service
Browse files Browse the repository at this point in the history
- Add `$resolve` service for exposing helper as an injectable
  • Loading branch information
wesleycho committed Jan 1, 2016
1 parent 2aad08d commit 70c6659
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/common/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ export function tail(collection: any[]): any {
* in your angular app (use {@link ui.router} module instead).
*
*/
angular.module('ui.router.util', ['ng', 'ui.router.init']);
angular.module('ui.router.util', ['ng', 'ui.router.init', 'ui.router.resolve']);

/**
* @ngdoc overview
Expand Down
30 changes: 27 additions & 3 deletions src/ng1/angular1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@
import {IQService} from "angular";
import {Router} from "../router";
import {services} from "../common/coreservices";
import {isObject} from "../common/common";
import {forEach, isObject} from "../common/common";
import {RawParams} from "../params/interface";
import {Node} from "../path/module";
import {Resolvables} from "../resolve/interface";
import {Resolvable, ResolveContext} from "../resolve/module";
import {State} from "../state/module";

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

Expand Down Expand Up @@ -80,21 +85,40 @@ function ng1UIRouter($locationProvider) {
};

bindFunctions(["replace", "url", "path", "search", "hash"], $location, services.location);
bindFunctions([ 'port', 'protocol', 'host'], $location, services.locationConfig);
bindFunctions(['port', 'protocol', 'host'], $location, services.locationConfig);
bindFunctions(['baseHref'], $browser, services.locationConfig);

return router;
}
}

function resolveFactory() {
return {
resolve: (invocables, locals, parent, self) => {
let state = new State({ params: {} });
let node = new Node(state, <RawParams> {});
let context = new ResolveContext([node]);
let resolvables: Resolvables = {};
forEach(invocables, (invocable, key) => {
resolvables[key] = new Resolvable(`${key}`, invocable);
});

context.addResolvables(resolvables, node.state);

return context.resolvePath();
}
};
}

angular.module('ui.router.init', []).provider("ng1UIRouter", <any> ng1UIRouter);
// Register as a provider so it's available to other providers
angular.module('ui.router.util').provider('$urlMatcherFactory', ['ng1UIRouterProvider', () => router.urlMatcherFactory]);
angular.module('ui.router.router').provider('$urlRouter', ['ng1UIRouterProvider', () => router.urlRouterProvider]);
angular.module('ui.router.state').provider('$state', ['ng1UIRouterProvider', () => router.stateProvider]);
angular.module('ui.router.resolve', []).factory('$resolve', <any> resolveFactory);

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

0 comments on commit 70c6659

Please sign in to comment.