Skip to content

Commit

Permalink
Merge pull request #2 from jyonker/master
Browse files Browse the repository at this point in the history
Added keepOriginalNames flag to prevent conversion to dot notation.
  • Loading branch information
Mark Lagendijk committed Mar 18, 2014
2 parents 9e3da48 + f1d561c commit 85ec57d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 25 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,25 @@ angular.module('myApp', ['ui.router', 'ui.router.stateHelper'])
});
});
```

## Options
### Dot notation name conversion
By default, all state names are converted to use ui-router's dot notation (e.g. `parentStateName.childStateName`).
This can be disabled by calling `.setNestedState()` with an optional second parameter of `true`.
For example:

``` javascript
angular.module('myApp', ['ui.router', 'ui.router.stateHelper'])
.config(function(stateHelperProvider){
stateHelperProvider.setNestedState({
name: 'root',
templateUrl: 'root.html',
children: [
{
name: 'contacts',
templateUrl: 'contacts.html'
}
]
}, true);
});
```
9 changes: 6 additions & 3 deletions statehelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ angular.module('ui.router.stateHelper', ['ui.router'])
*
* @param {Object} state - A regular ui.router state object.
* @param {Array} [state.children] - An optional array of child states.
* @param {Boolean} keepOriginalNames - An optional flag that prevents conversion of names to dot notation if true.
*/
this.setNestedState = function(state){
fixStateName(state);
this.setNestedState = function(state, keepOriginalNames){
if (!keepOriginalNames){
fixStateName(state);
}
$stateProvider.state(state);

if(state.children && state.children.length){
state.children.forEach(function(childState){
childState.parent = state;
self.setNestedState(childState);
self.setNestedState(childState, keepOriginalNames);
});
}
};
Expand Down
2 changes: 1 addition & 1 deletion statehelper.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

83 changes: 62 additions & 21 deletions test/statehelperSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,36 @@ describe('ui-router.stateHelper', function(){
]
};

expectedState = {
name: 'root',
children: [
{
name: 'root.login',
templateUrl: '/partials/views/#.html'
},
{
name: 'root.backup',
children: [
{
name: 'root.backup.dashboard'
}
]
}
]
};

spyOn($stateProvider, 'state');
stateHelperProvider.setNestedState(rootState);
}));

describe('.setNestedState', function(){
beforeEach(inject(function(){
expectedState = {
name: 'root',
children: [
{
name: 'root.login',
templateUrl: '/partials/views/#.html'
},
{
name: 'root.backup',
children: [
{
name: 'root.backup.dashboard'
}
]
}
]
};

stateHelperProvider.setNestedState(rootState);
}));

it('should set each state', function(){
expect($stateProvider.state.callCount).toBe(4);
});


it('should convert names to dot notation, set parent references', function(){
// Since the states are objects which contain references to each other, we are testing the eventual
// root state object (and not the root state object as it is passed to $stateProvider.$state).
Expand All @@ -66,4 +68,43 @@ describe('ui-router.stateHelper', function(){
expect($stateProvider.state.argsForCall[0][0]).toEqual(expectedState);
});
});
});

describe('.setNestedState with keepOriginalNames set to true', function(){
beforeEach(inject(function(){
expectedState = {
name: 'root',
children: [
{
name: 'login',
templateUrl: '/partials/views/#.html'
},
{
name: 'backup',
children: [
{
name: 'dashboard'
}
]
}
]
};

stateHelperProvider.setNestedState(rootState, true);
}));

it('should not convert names to dot notation, set parent references', function(){
// Since the states are objects which contain references to each other, we are testing the eventual
// root state object (and not the root state object as it is passed to $stateProvider.$state).
// Because of this we have to test everything at once

expectedState.children[0].parent = expectedState;
expectedState.children[1].parent = expectedState;
expectedState.children[1].children[0].parent = expectedState.children[1];

expect($stateProvider.state.argsForCall[0][0]).toEqual(expectedState);
});
});
});



0 comments on commit 85ec57d

Please # to comment.