Skip to content

Commit ff47736

Browse files
alexandrbarannateabele
authored andcommitted
'data' property inheritance/override in chain of states
1 parent 88b8fd1 commit ff47736

File tree

2 files changed

+34
-9
lines changed

2 files changed

+34
-9
lines changed

src/state.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
1616
return compositeName ? findState(compositeName[1]) : root;
1717
},
1818

19+
// inherit 'data' from parent and override by own values (if any)
20+
data: function(state) {
21+
if (state.parent && state.parent.data) {
22+
state.data = state.self.data = angular.extend({}, state.parent.data, state.data);
23+
}
24+
return state.data;
25+
},
26+
1927
// Build a URLMatcher if necessary, either via a relative or absolute URL
2028
url: function(state) {
2129
var url = state.url;
@@ -84,15 +92,6 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
8492
if (own) ownParams.push(p);
8593
});
8694
return ownParams;
87-
},
88-
89-
data: function(state) {
90-
// inherit 'data' from parent and override by own values (if any)
91-
if (state.parent && state.parent.data) {
92-
state.data = angular.extend({}, state.parent.data, state.data);
93-
state.self.data = state.data;
94-
}
95-
return state.data;
9695
}
9796
};
9897

test/stateSpec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,4 +357,30 @@ describe('state', function () {
357357
expect($state.$current.resolve).toEqual({});
358358
}));
359359
});
360+
361+
describe(' "data" property inheritance/override', function () {
362+
it('"data" property should stay immutable for if state doesn\'t have parent', inject(function ($state) {
363+
initStateTo(H);
364+
expect($state.current.name).toEqual('H');
365+
expect($state.current.data.propA).toEqual(H.data.propA);
366+
expect($state.current.data.propB).toEqual(H.data.propB);
367+
}));
368+
369+
it('"data" property should be inherited from parent if state doesn\'t define it', inject(function ($state) {
370+
initStateTo(HH);
371+
expect($state.current.name).toEqual('HH');
372+
expect($state.current.data.propA).toEqual(H.data.propA);
373+
expect($state.current.data.propB).toEqual(H.data.propB);
374+
}));
375+
376+
it('"data" property should be overridden/extended if state defines it', inject(function ($state) {
377+
initStateTo(HHH);
378+
expect($state.current.name).toEqual('HHH');
379+
expect($state.current.data.propA).toEqual(HHH.data.propA);
380+
expect($state.current.data.propB).toEqual(H.data.propB);
381+
expect($state.current.data.propB).toEqual(HH.data.propB);
382+
expect($state.current.data.propC).toEqual(HHH.data.propC);
383+
}));
384+
});
385+
360386
});

0 commit comments

Comments
 (0)