Skip to content

Commit

Permalink
Updated queue to work when parent is declared as a property on the st…
Browse files Browse the repository at this point in the history
…ate. Added test for cross-module implementation.
  • Loading branch information
theJoeBiz committed Oct 11, 2013
1 parent 4644b42 commit 104494e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 12 deletions.
15 changes: 9 additions & 6 deletions src/state.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$StateProvider.$inject = ['$urlRouterProvider', '$urlMatcherFactoryProvider', '$locationProvider'];
function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $locationProvider) {

var root, states = {}, $state, queue = [];
var root, states = {}, $state, queue = {};

// Builds state properties from definition passed to registerState()
var stateBuilder = {
Expand Down Expand Up @@ -163,12 +163,15 @@ function $StateProvider( $urlRouterProvider, $urlMatcherFactory, $
if (!isString(name) || name.indexOf('@') >= 0) throw new Error("State must have a valid name");
if (states[name]) throw new Error("State '" + name + "'' is already defined");

// Get parent name
var parentName =
(name.indexOf('.') !== -1) ? name.substring(0, name.lastIndexOf('.'))
: (isString(state.parent)) ? state.parent
: '';

// If parent is not registered yet, add state to queue and register later
if (name.indexOf('.') !== -1) {
var parentName = name.substring(0, name.lastIndexOf('.'));
if (!states[parentName]) {
return queueState(parentName, state.self);
}
if (parentName && !states[parentName]) {
return queueState(parentName, state.self);
}

for (var key in stateBuilder) {
Expand Down
41 changes: 35 additions & 6 deletions test/stateSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ describe('state', function () {
H = { data: {propA: 'propA', propB: 'propB'} },
HH = { parent: H },
HHH = {parent: HH, data: {propA: 'overriddenA', propC: 'propC'} },
J = {},
JJ = {},
AppInjectable = {};

beforeEach(module(function ($stateProvider, $provide) {
Expand All @@ -47,8 +45,6 @@ describe('state', function () {
.state('H', H)
.state('HH', HH)
.state('HHH', HHH)
.state('JJ.J', J)
.state('JJ', JJ)

.state('home', { url: "/" })
.state('home.item', { url: "front/:id" })
Expand Down Expand Up @@ -542,8 +538,6 @@ describe('state', function () {
'H',
'HH',
'HHH',
'JJ',
'JJ.J',
'about',
'about.person',
'about.person.item',
Expand Down Expand Up @@ -721,3 +715,38 @@ describe('state', function () {

});
});

describe('state queue', function(){
angular.module('ui.router.queue.test', ['ui.router.queue.test.dependency'])
.config(function($stateProvider) {
$stateProvider
.state('queue-test-a', {})
.state('queue-test-b-child', { parent: 'queue-test-b' })
.state('queue-test-b', {});
});
angular.module('ui.router.queue.test.dependency', [])
.config(function($stateProvider) {
$stateProvider
.state('queue-test-a.child', {})
});

var expectedStates = ['','queue-test-a', 'queue-test-a.child', 'queue-test-b', 'queue-test-b-child'];

it('should work across modules', function() {
module('ui.router.queue.test', 'ui.router.queue.test.dependency');

inject(function ($state) {
var list = $state.get().sort(function(a, b) { return (a.name > b.name) - (b.name > a.name); });
expect(list.map(function(state) { return state.name; })).toEqual(expectedStates);
});
});

it('should work when parent is name string', function() {
module('ui.router.queue.test', 'ui.router.queue.test.dependency');

inject(function ($state) {
var list = $state.get().sort(function(a, b) { return (a.name > b.name) - (b.name > a.name); });
expect(list.map(function(state) { return state.name; })).toEqual(expectedStates);
});
});
});

0 comments on commit 104494e

Please sign in to comment.