Permalink
Browse files

router navigates to the default route if nothing is present in the ha…

…shmark
  • Loading branch information...
1 parent 6fffa46 commit a5be21311f49c1ad3a3de235065f4ebb4df96732 @podefr podefr committed Sep 7, 2013
Showing with 90 additions and 18 deletions.
  1. +42 −10 specs/LocationRouter.spec.js
  2. +48 −8 src/LocationRouter.js
@@ -28,6 +28,24 @@ require(["LocationRouter", "Router"], function (LocationRouter, Router) {
locationRouter = new LocationRouter();
});
+ it("has a default route to navigate to when none is supplied in the url", function () {
+ expect(locationRouter.getDefaultRoute).toBeInstanceOf(Function);
+ expect(locationRouter.getDefaultRoute()).toBe("");
+
+ expect(locationRouter.setDefaultRoute).toBeInstanceOf(Function);
+ expect(locationRouter.setDefaultRoute()).toBe(false);
+ expect(locationRouter.setDefaultRoute("")).toBe(false);
+ expect(locationRouter.setDefaultRoute("default")).toBe(true);
+
+ expect(locationRouter.getDefaultRoute()).toBe("default");
+ });
+
+ it("can be given the default route on start", function () {
+ locationRouter.start("home");
+
+ expect(locationRouter.getDefaultRoute()).toBe("home");
+ });
+
it("has a default function for parsing a hashtag", function () {
expect(locationRouter.parse).toBeInstanceOf(Function);
@@ -55,6 +73,16 @@ require(["LocationRouter", "Router"], function (LocationRouter, Router) {
expect(locationRouter.navigate).toHaveBeenCalledWith("hello", "im", "the", "router");
});
+ it("navigates to the default route if no route is defined in the url", function () {
+ window.location.hash = "";
+
+ spyOn(locationRouter, "navigate");
+
+ locationRouter.start("default");
+
+ expect(locationRouter.navigate).toHaveBeenCalledWith("default");
+ });
+
it("listens to hash change on start", function () {
spyOn(locationRouter, "bindOnHashChange");
@@ -73,15 +101,27 @@ require(["LocationRouter", "Router"], function (LocationRouter, Router) {
it("parses the new hash and navigates to the corresponding route when the hash changes", function () {
var array = ["hello", "im", "the", "router"];
+ window.location.hash = "hello/im/the/router";
spyOn(locationRouter, "parse").andReturn(array);
spyOn(locationRouter, "navigate");
- locationRouter.onHashChange({ newURL: "url#hello/im/the/router"});
+ locationRouter.onHashChange();
- expect(locationRouter.parse).toHaveBeenCalledWith("hello/im/the/router");
+ expect(locationRouter.parse).toHaveBeenCalledWith(window.location.hash);
expect(locationRouter.navigate).toHaveBeenCalledWith("hello", "im", "the", "router");
});
+ it("navigates to the default route if the hash is empty", function () {
+ window.location.hash = "";
+
+ spyOn(locationRouter, "navigate");
+ locationRouter.setDefaultRoute("default");
+
+ locationRouter.onHashChange();
+
+ expect(locationRouter.navigate).toHaveBeenCalledWith("default");
+ });
+
});
describe("LocationRouter updates the URL when navigating", function () {
@@ -123,14 +163,6 @@ require(["LocationRouter", "Router"], function (LocationRouter, Router) {
expect(window.location.hash).toBe("#hello/im/the/router");
});
- it("clears the previous history on start so the route coming from the url is the initial one", function () {
- spyOn(locationRouter, "clearHistory");
-
- locationRouter.start();
-
- expect(locationRouter.clearHistory).toHaveBeenCalled();
- });
-
});
describe("LocationRouter can be destroyed", function () {
View
@@ -21,7 +21,49 @@ function LocationRouter(Router, Tools) {
* The handle on the watch
* @private
*/
- var _watchHandle;
+ var _watchHandle,
+
+ /**
+ * The default route to navigate to when nothing is supplied in the url
+ * @private
+ */
+ _defaultRoute = "";
+
+ /**
+ * Navigates to the current hash or to the default route if none is supplied in the url
+ * @private
+ */
+ /*jshint validthis:true*/
+ function doNavigate() {
+ if (window.location.hash) {
+ var parsedHash = this.parse(window.location.hash);
+ this.navigate.apply(this, parsedHash);
+ } else {
+ this.navigate(_defaultRoute);
+ }
+ }
+
+ /**
+ * Set the default route to navigate to when nothing is defined in the url
+ * @param {String} defaultRoute the defaultRoute to navigate to
+ * @returns {Boolean} true if it's not an empty string
+ */
+ this.setDefaultRoute = function setDefaultRoute(defaultRoute) {
+ if (defaultRoute && typeof defaultRoute == "string") {
+ _defaultRoute = defaultRoute;
+ return true;
+ } else {
+ return false;
+ }
+ };
+
+ /**
+ * Get the currently set default route
+ * @returns {String} the default route
+ */
+ this.getDefaultRoute = function getDefaultRoute() {
+ return _defaultRoute;
+ };
/**
* The function that parses the url to determine the route to navigate to.
@@ -59,10 +101,9 @@ function LocationRouter(Router, Tools) {
* It will also start listening to route changes and hashmark changes to navigate.
* While navigating, the hashmark itself will also change to reflect the current route state
*/
- this.start = function start() {
- this.clearHistory();
- var parsedHash = this.parse(window.location.hash);
- this.navigate.apply(this, parsedHash);
+ this.start = function start(defaultRoute) {
+ this.setDefaultRoute(defaultRoute);
+ doNavigate.call(this);
this.bindOnHashChange();
this.bindOnRouteChange();
};
@@ -79,9 +120,8 @@ function LocationRouter(Router, Tools) {
* Parse the hash and navigate to the corresponding url
* @private
*/
- this.onHashChange = function onHashChange(event) {
- var parsedHash = this.parse(event.newURL.split("#").pop());
- this.navigate.apply(this, parsedHash);
+ this.onHashChange = function onHashChange() {
+ doNavigate.call(this);
};
/**

0 comments on commit a5be213

Please sign in to comment.