UNPKG

3.2 kBJavaScriptView Raw
1/** @publicapi @module ng1 */ /** */
2import { StateObject, PathNode, ResolveContext, mapObj, resolvablesBuilder } from '@uirouter/core';
3import * as angular from 'angular';
4/**
5 * Implementation of the legacy `$resolve` service for angular 1.
6 */
7var $resolve = {
8 /**
9 * Asynchronously injects a resolve block.
10 *
11 * This emulates most of the behavior of the ui-router 0.2.x $resolve.resolve() service API.
12 *
13 * ### Not bundled by default
14 *
15 * This API is no longer not part of the standard `@uirouter/angularjs` bundle.
16 * For users of the prebuilt bundles, add the `release/resolveService.min.js` UMD bundle.
17 * For bundlers (webpack, browserify, etc), add `@uirouter/angularjs/lib/legacy/resolveService`.
18 *
19 * ---
20 *
21 * Given an object `invocables`, where keys are strings and values are injectable functions,
22 * injects each function, and waits for the resulting promise to resolve.
23 * When all resulting promises are resolved, returns the results as an object.
24 *
25 * #### Example:
26 * ```js
27 * let invocables = {
28 * foo: [ '$http', ($http) =>
29 * $http.get('/api/foo').then(resp => resp.data) ],
30 * bar: [ 'foo', '$http', (foo, $http) =>
31 * $http.get('/api/bar/' + foo.barId).then(resp => resp.data) ]
32 * }
33 * $resolve.resolve(invocables)
34 * .then(results => console.log(results.foo, results.bar))
35 * // Logs foo and bar:
36 * // { id: 123, barId: 456, fooData: 'foo data' }
37 * // { id: 456, barData: 'bar data' }
38 * ```
39 *
40 * @param invocables an object which looks like an [[StateDeclaration.resolve]] object; keys are resolve names and values are injectable functions
41 * @param locals key/value pre-resolved data (locals)
42 * @param parent a promise for a "parent resolve"
43 */
44 resolve: function (invocables, locals, parent) {
45 if (locals === void 0) { locals = {}; }
46 var parentNode = new PathNode(new StateObject({ params: {}, resolvables: [] }));
47 var node = new PathNode(new StateObject({ params: {}, resolvables: [] }));
48 var context = new ResolveContext([parentNode, node]);
49 context.addResolvables(resolvablesBuilder({ resolve: invocables }), node.state);
50 var resolveData = function (parentLocals) {
51 var rewrap = function (_locals) { return resolvablesBuilder({ resolve: mapObj(_locals, function (local) { return function () { return local; }; }) }); };
52 context.addResolvables(rewrap(parentLocals), parentNode.state);
53 context.addResolvables(rewrap(locals), node.state);
54 var tuples2ObjR = function (acc, tuple) {
55 acc[tuple.token] = tuple.value;
56 return acc;
57 };
58 return context.resolvePath().then(function (results) { return results.reduce(tuples2ObjR, {}); });
59 };
60 return parent ? parent.then(resolveData) : resolveData({});
61 },
62};
63/** @hidden */
64export var resolveFactory = function () { return $resolve; };
65// The old $resolve service
66angular.module('ui.router').factory('$resolve', resolveFactory);
67//# sourceMappingURL=resolveService.js.map
\No newline at end of file