Skip to content

Commit

Permalink
Support basic States inside of Routes
Browse files Browse the repository at this point in the history
  • Loading branch information
wagenet committed Jun 26, 2012
1 parent e271aff commit 8fd57b3
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
16 changes: 14 additions & 2 deletions packages/ember-routing/lib/routable.js
Expand Up @@ -78,7 +78,7 @@ Ember.Routable = Ember.Mixin.create({
In general, this will update the browser's URL.
*/
updateRoute: function(manager, location) {
if (get(this, 'isLeaf')) {
if (get(this, 'isLeafRoute')) {
var path = this.absoluteRoute(manager);
location.setURL(path);
}
Expand Down Expand Up @@ -130,6 +130,16 @@ Ember.Routable = Ember.Mixin.create({
return typeof get(this, 'route') === 'string';
}).cacheable(),

/**
@private
Determine if this is the last routeable state
*/
isLeafRoute: Ember.computed(function() {
if (get(this, 'isLeaf')) { return true; }
return !get(this, 'childStates').findProperty('isRoutable');
}).cacheable(),

/**
@private
Expand Down Expand Up @@ -272,10 +282,12 @@ Ember.Routable = Ember.Mixin.create({
on the state whose path is `/posts` with the path `/2/comments`.
*/
routePath: function(manager, path) {
if (get(this, 'isLeaf')) { return; }
if (get(this, 'isLeafRoute')) { return; }

var childStates = get(this, 'childStates'), match;

childStates = Ember.A(childStates.filterProperty('isRoutable'));

childStates = childStates.sort(function(a, b) {
var aDynamicSegments = getPath(a, 'routeMatcher.identifiers.length'),
bDynamicSegments = getPath(b, 'routeMatcher.identifiers.length'),
Expand Down
17 changes: 15 additions & 2 deletions packages/ember-routing/lib/router.js
Expand Up @@ -389,21 +389,34 @@ Ember.Router = Ember.StateManager.extend(
route: function(path) {
set(this, 'isRouting', true);

var routableState;

try {
path = path.replace(/^(?=[^\/])/, "/");

this.send('navigateAway');
this.send('unroutePath', path);

var currentURL = get(this, 'currentState').absoluteRoute(this);
routableState = get(this, 'currentState');
while (routableState && !routableState.get('isRoutable')) {
routableState = get(routableState, 'parentState');
}
var currentURL = routableState ? routableState.absoluteRoute(this) : '';
var rest = path.substr(currentURL.length);

this.send('routePath', rest);
} finally {
set(this, 'isRouting', false);
}

get(this, 'currentState').updateRoute(this, get(this, 'location'));
routableState = get(this, 'currentState');
while (routableState && !routableState.get('isRoutable')) {
routableState = get(routableState, 'parentState');
}

if (routableState) {
routableState.updateRoute(this, get(this, 'location'));
}
},

urlFor: function(path, hash) {
Expand Down
40 changes: 40 additions & 0 deletions packages/ember-routing/tests/routable_test.js
Expand Up @@ -142,6 +142,46 @@ test("when you descend into a state, the route is set", function() {
router.send('ready');
});

test("when you descend into a state, the route is set even when child states (not routes) are present", function() {
var state = Ember.Route.create({
ready: function(manager) {
manager.transitionTo('fooChild.barChild.bazChild');
},

fooChild: Ember.Route.create({
route: 'foo',

barChild: Ember.Route.create({
route: 'bar',

bazChild: Ember.Route.create({
route: 'baz',

basicState: Ember.State.create()
})
})
})
});

var count = 0;

var router = Ember.Router.create({
root: state,
location: {
setURL: function(url) {
if (count === 0) {
equal(url, '/foo/bar/baz', "The current URL should be passed in");
count++;
} else {
ok(false, "Should not get here");
}
}
}
});

router.send('ready');
});

var router;
var Post = {
find: function(id) {
Expand Down
27 changes: 26 additions & 1 deletion packages/ember-routing/tests/router_test.js
Expand Up @@ -334,7 +334,32 @@ test("should update route for redirections", function() {
})
});

router.route('/');
Ember.run(function() {
router.route('/');
});

equal(location.url, '/login');
});

test("respects initialState if leafRoute with child states", function() {
var router = Ember.Router.create({
location: location,
namespace: namespace,
root: Ember.Route.create({
foo: Ember.Route.create({
route: '/foo',

initialState: 'bar',

bar: Ember.State.create()
})
})
});

Ember.run(function() {
router.route('/foo');
});

equal(location.url, '/foo');
equal(router.getPath('currentState.name'), 'bar');
});

0 comments on commit 8fd57b3

Please sign in to comment.