Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory) {
parent = findState(state.parent);
}
state.parent = parent;
// inherit 'data' from parent and override by own values (if any)
if (state.parent && state.parent.data) {
state.data = angular.extend({}, state.parent.data, state.data);
state.self.data = state.data;
}
// state.children = [];
// if (parent) parent.children.push(state);

Expand Down
34 changes: 33 additions & 1 deletion test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@ describe('state', function () {
D = { params: [ 'x', 'y' ] },
DD = { parent: D, params: [ 'x', 'y', 'z' ] },
E = { params: [ 'i' ] },
H = { data: {propA: 'propA', propB: 'propB'} },
HH = { parent: H },
HHH = {parent: HH, data: {propA: 'overriddenA', propC: 'propC'} }
AppInjectable = {};

beforeEach(module(function ($stateProvider, $provide) {
angular.forEach([ A, B, C, D, DD, E ], function (state) {
angular.forEach([ A, B, C, D, DD, E, H, HH, HHH ], function (state) {
state.onEnter = callbackLogger('onEnter');
state.onExit = callbackLogger('onExit');
});
Expand All @@ -33,6 +36,9 @@ describe('state', function () {
.state('D', D)
.state('DD', DD)
.state('E', E)
.state('H', H)
.state('HH', HH)
.state('HHH', HHH)

.state('home', { url: "/" })
.state('home.item', { url: "front/:id" })
Expand Down Expand Up @@ -275,4 +281,30 @@ describe('state', function () {
expect($state.href("about.person.item", { person: "bob", id: null })).toEqual("/about/bob/");
}));
});

describe(' "data" property inheritance/override', function () {
it('"data" property should stay immutable for if state doesn\'t have parent', inject(function ($state) {
initStateTo(H);
expect($state.current.name).toEqual('H');
expect($state.current.data.propA).toEqual(H.data.propA);
expect($state.current.data.propB).toEqual(H.data.propB);
}));

it('"data" property should be inherited from parent if state doesn\'t define it', inject(function ($state) {
initStateTo(HH);
expect($state.current.name).toEqual('HH');
expect($state.current.data.propA).toEqual(H.data.propA);
expect($state.current.data.propB).toEqual(H.data.propB);
}));

it('"data" property should be overridden/extended if state defines it', inject(function ($state) {
initStateTo(HHH);
expect($state.current.name).toEqual('HHH');
expect($state.current.data.propA).toEqual(HHH.data.propA);
expect($state.current.data.propB).toEqual(H.data.propB);
expect($state.current.data.propB).toEqual(HH.data.propB);
expect($state.current.data.propC).toEqual(HHH.data.propC);
}));
});

});