Skip to content

Commit 6ffe878

Browse files
refactor($stateParams): Inject $stateParams using Resolvable instead of locals
1 parent 1954495 commit 6ffe878

File tree

7 files changed

+14
-15
lines changed

7 files changed

+14
-15
lines changed

src/path/pathFactory.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ export default class PathFactory {
4848
/** Given an IParamsNode, make an IResolveNode by creating resolvables for resolves on the state's declaration */
4949
static makeResolveNode(node: IParamsNode): IResolveNode {
5050
const makeResolvable = (_node: INode) => (resolveFn: Function, name: string) => new Resolvable(name, resolveFn, _node.state);
51-
let resolvables = {ownResolvables: map(node.state.resolve || {}, makeResolvable(node)) };
52-
return extend({}, node, resolvables);
51+
let ownResolvables = map(node.state.resolve || {}, makeResolvable(node));
52+
return extend({}, node, {ownResolvables});
5353
}
5454

5555
/** Given a fromPath: ITransPath and a TargetState, builds a toPath: IParamsPath */
@@ -120,11 +120,12 @@ export default class PathFactory {
120120
// Attach bound resolveContext and paramValues to each node
121121
// Attach views to each node
122122
transPath.nodes().forEach((node: ITransNode) => {
123-
node.resolveContext = resolveContext.isolateRootTo(node.state);
124-
node.resolveInjector = new ResolveInjector(node.resolveContext, node.state);
125-
node.paramValues = paramValues.$isolateRootTo(node.state.name);
126-
node.views = makeViews(node);
127-
}
123+
node.resolveContext = resolveContext.isolateRootTo(node.state);
124+
node.resolveInjector = new ResolveInjector(node.resolveContext, node.state);
125+
node.paramValues = paramValues.$isolateRootTo(node.state.name);
126+
node.ownResolvables["$stateParams"] = new Resolvable("$stateParams", () => node.paramValues, node.state);
127+
node.views = makeViews(node);
128+
}
128129
);
129130

130131
return transPath;

src/transition/hookBuilder.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ export default class HookBuilder {
135135

136136
/** Given a node and a callback function, builds a TransitionHook */
137137
buildHook(node: ITransNode, fn: IInjectable, moreLocals?, options: ITransitionHookOptions = {}): TransitionHook {
138-
let nodeLocals = { $stateParams: node.paramValues };
139-
let locals = extend({}, this.transitionLocals, nodeLocals, moreLocals);
138+
let locals = extend({}, this.transitionLocals, moreLocals);
140139
let _options = extend({}, this.baseHookOptions, options);
141140

142141
return new TransitionHook(node.state, fn, locals, node.resolveContext, _options);

src/view/templateFactory.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -97,13 +97,12 @@ function $TemplateFactory( $http, $templateCache) {
9797
* Creates a template by invoking an injectable provider function.
9898
*
9999
* @param {Function} provider Function to invoke via `locals`
100-
* @param {Object} params Parameters for the template.
101100
* @param {Function} injectFn a function used to invoke the template provider
102101
* @return {string|Promise.<string>} The template html as a string, or a promise
103102
* for that string.
104103
*/
105104
this.fromProvider = function (provider, params, injectFn) {
106-
return injectFn(provider, { $stateParams: params });
105+
return injectFn(provider);
107106
};
108107
}
109108

src/view/viewDirective.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ function $ViewDirectiveFill ( $compile, $controller, $interpolate, $injec
287287

288288
if (controller) {
289289
let locals = data.$locals;
290-
let controllerInstance = $controller(controller, extend(locals, { $scope: scope })); // $stateParams?
290+
let controllerInstance = $controller(controller, extend(locals, { $scope: scope }));
291291
if (controllerAs) scope[controllerAs] = controllerInstance;
292292
$element.data('$ngControllerController', controllerInstance);
293293
$element.children().data('$ngControllerController', controllerInstance);

test/resolveSpec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ describe('Resolvables system:', function () {
159159
let ctx = new ResolveContext(path);
160160
let resolvableLocals = ctx.getResolvables(statesMap["C"]);
161161
let keys = Object.keys(resolvableLocals).sort();
162-
expect(keys).toEqual( ["_A", "_A2", "_B", "_B2", "_C", "_C2" ] );
162+
expect(keys).toEqual( ["$stateParams", "_A", "_A2", "_B", "_B2", "_C", "_C2" ] );
163163
}));
164164
});
165165

test/transitionSpec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ describe('transition', function () {
8080
queue.flush($state);
8181
makeTransition = function makeTransition(from, to, options) {
8282
var paramsPath = PathFactory.makeParamsPath(targetState(from));
83-
var fromPath = PathFactory.bindTransNodesToPath(paramsPath);
83+
var fromPath = PathFactory.bindTransNodesToPath(paramsPath.adapt(PathFactory.makeResolveNode));
8484
return $transitions.create(fromPath, targetState(to, null, options));
8585
};
8686
}));

test/viewSpec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ describe('view', function() {
5555
beforeEach(() => {
5656
state = register({ name: "foo" });
5757
var nodes = [root, state].map(_state => ({ state: _state, ownParams: {}}));
58-
var path = PathFactory.bindTransNodesToPath(<any> new Path(nodes));
58+
var path = PathFactory.bindTransNodesToPath(<any> new Path(nodes).adapt(PathFactory.makeResolveNode));
5959
ctx = new ResolveContext(path);
6060
});
6161

0 commit comments

Comments
 (0)