Skip to content

Commit

Permalink
Merge pull request tildeio#17 from mhelabs/reset-router
Browse files Browse the repository at this point in the history
Router#reset exits & clears current route handlers
  • Loading branch information
stefanpenner committed May 18, 2013
2 parents 18dd82e + 931176f commit 2279d35
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 0 deletions.
15 changes: 15 additions & 0 deletions dist/router.amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,21 @@ define("router",
return this.recognizer.hasRoute(route);
},

/**
Clears the current and target route handlers and triggers exit
on each of them starting at the leaf and traversing up through
its ancestors.
*/
reset: function() {
eachHandler(this.currentHandlerInfos, function(handler) {
if (handler.exit) {
handler.exit();
}
});
this.currentHandlerInfos = null;
this.targetHandlerInfos = null;
},

/**
The entry point for handling a change to the URL (usually
via the back and forward button).
Expand Down
15 changes: 15 additions & 0 deletions dist/router.cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ Router.prototype = {
return this.recognizer.hasRoute(route);
},

/**
Clears the current and target route handlers and triggers exit
on each of them starting at the leaf and traversing up through
its ancestors.
*/
reset: function() {
eachHandler(this.currentHandlerInfos, function(handler) {
if (handler.exit) {
handler.exit();
}
});
this.currentHandlerInfos = null;
this.targetHandlerInfos = null;
},

/**
The entry point for handling a change to the URL (usually
via the back and forward button).
Expand Down
15 changes: 15 additions & 0 deletions dist/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@
return this.recognizer.hasRoute(route);
},

/**
Clears the current and target route handlers and triggers exit
on each of them starting at the leaf and traversing up through
its ancestors.
*/
reset: function() {
eachHandler(this.currentHandlerInfos, function(handler) {
if (handler.exit) {
handler.exit();
}
});
this.currentHandlerInfos = null;
this.targetHandlerInfos = null;
},

/**
The entry point for handling a change to the URL (usually
via the back and forward button).
Expand Down
15 changes: 15 additions & 0 deletions lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,21 @@ Router.prototype = {
return this.recognizer.hasRoute(route);
},

/**
Clears the current and target route handlers and triggers exit
on each of them starting at the leaf and traversing up through
its ancestors.
*/
reset: function() {
eachHandler(this.currentHandlerInfos, function(handler) {
if (handler.exit) {
handler.exit();
}
});
this.currentHandlerInfos = null;
this.targetHandlerInfos = null;
},

/**
The entry point for handling a change to the URL (usually
via the back and forward button).
Expand Down
28 changes: 28 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1703,3 +1703,31 @@ test("A final handler can specify an additional non-routable handler", function(
router.handleURL("/index");
router.transitionTo('showPost', { id: 1 });
});

test("reset exits and clears the current and target route handlers", function() {
var postIndexExited = false;
var showAllPostsExited = false;

var postIndexHandler = {
exit: function() {
postIndexExited = true;
}
};
var showAllPostsHandler = {
exit: function() {
showAllPostsExited = true;
}
};
handlers = {
postIndex: postIndexHandler,
showAllPosts: showAllPostsHandler
};

router.handleURL("/posts/all");
router.reset();

ok(postIndexExited, "Post index handler did not exit");
ok(showAllPostsExited, "Show all posts handler did not exit");
equal(router.currentHandlerInfos, null, "currentHandlerInfos should be null");
equal(router.targetHandlerInfos, null, "targetHandlerInfos should be null");
});

0 comments on commit 2279d35

Please sign in to comment.