Skip to content

Commit 2ce9921

Browse files
committed
Merge pull request #253 from abaran/data-property-inheritance-override
'data' property inheritance/override in chain of states
2 parents 87947a9 + ac8e656 commit 2ce9921

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

src/state.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
4040
parent = findState(state.parent);
4141
}
4242
state.parent = parent;
43+
// inherit 'data' from parent and override by own values (if any)
44+
if (state.parent && state.parent.data) {
45+
state.data = angular.extend({}, state.parent.data, state.data);
46+
state.self.data = state.data;
47+
}
4348
// state.children = [];
4449
// if (parent) parent.children.push(state);
4550

test/stateSpec.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,13 @@ describe('state', function () {
1818
D = { params: [ 'x', 'y' ] },
1919
DD = { parent: D, params: [ 'x', 'y', 'z' ] },
2020
E = { params: [ 'i' ] },
21+
H = { data: {propA: 'propA', propB: 'propB'} },
22+
HH = { parent: H },
23+
HHH = {parent: HH, data: {propA: 'overriddenA', propC: 'propC'} }
2124
AppInjectable = {};
2225

2326
beforeEach(module(function ($stateProvider, $provide) {
24-
angular.forEach([ A, B, C, D, DD, E ], function (state) {
27+
angular.forEach([ A, B, C, D, DD, E, H, HH, HHH ], function (state) {
2528
state.onEnter = callbackLogger('onEnter');
2629
state.onExit = callbackLogger('onExit');
2730
});
@@ -33,6 +36,9 @@ describe('state', function () {
3336
.state('D', D)
3437
.state('DD', DD)
3538
.state('E', E)
39+
.state('H', H)
40+
.state('HH', HH)
41+
.state('HHH', HHH)
3642

3743
.state('home', { url: "/" })
3844
.state('home.item', { url: "front/:id" })
@@ -275,4 +281,30 @@ describe('state', function () {
275281
expect($state.href("about.person.item", { person: "bob", id: null })).toEqual("/about/bob/");
276282
}));
277283
});
284+
285+
describe(' "data" property inheritance/override', function () {
286+
it('"data" property should stay immutable for if state doesn\'t have parent', inject(function ($state) {
287+
initStateTo(H);
288+
expect($state.current.name).toEqual('H');
289+
expect($state.current.data.propA).toEqual(H.data.propA);
290+
expect($state.current.data.propB).toEqual(H.data.propB);
291+
}));
292+
293+
it('"data" property should be inherited from parent if state doesn\'t define it', inject(function ($state) {
294+
initStateTo(HH);
295+
expect($state.current.name).toEqual('HH');
296+
expect($state.current.data.propA).toEqual(H.data.propA);
297+
expect($state.current.data.propB).toEqual(H.data.propB);
298+
}));
299+
300+
it('"data" property should be overridden/extended if state defines it', inject(function ($state) {
301+
initStateTo(HHH);
302+
expect($state.current.name).toEqual('HHH');
303+
expect($state.current.data.propA).toEqual(HHH.data.propA);
304+
expect($state.current.data.propB).toEqual(H.data.propB);
305+
expect($state.current.data.propB).toEqual(HH.data.propB);
306+
expect($state.current.data.propC).toEqual(HHH.data.propC);
307+
}));
308+
});
309+
278310
});

0 commit comments

Comments
 (0)