From 35ddbadb4144014e601605e0ecdf95dbbdc3f5d2 Mon Sep 17 00:00:00 2001 From: Rhsy Date: Mon, 2 Apr 2012 15:05:34 -0700 Subject: [PATCH] router improvements --- README.md | 1 + router.js | 33 ++++++++++++++++++--------------- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index cac9658..a0afbd5 100644 --- a/README.md +++ b/README.md @@ -246,6 +246,7 @@ you can then register your object with the mediator and the messages that you ma - schema is now an object - mvc.Control events can now be given a priority to order them - more tests +- router uses HTML5 where possible and you can pass in parameters to hide the fragment #### v0.8 #### diff --git a/router.js b/router.js index 1f0278b..0080d8d 100644 --- a/router.js +++ b/router.js @@ -10,29 +10,28 @@ goog.provide('mvc.Router'); goog.require('goog.array'); goog.require('goog.events'); goog.require('goog.History'); +goog.require('goog.history.Html5History'); /** * @constructor + * + * @param {boolean=} opt_noFragment set to true to hide fragment when using + * HTML5 history + * @param {string=} opt_blankPage url to a blank page - needed if HTML5 is not + * available and you don't want to show the fragment */ -mvc.Router = function() { - this.checkhistorysupport(); +mvc.Router = function(opt_noFragment, opt_blankPage) { + this.history_ = goog.history.Html5History.isSupported()? + new goog.history.Html5History(): + new goog.History(opt_noFragment, opt_blankPage); + if(this.history_.setUseFragment) + this.history_.setUseFragment(!opt_noFragment); goog.events.listen(this.history_, goog.history.EventType.NAVIGATE, this.onChange_, false, this); this.history_.setEnabled(true); this.routes_ = []; }; -/** -*checks if HTML5History is supported -*/ -mvc.prototype.checkhistorysupport=function (){ - if(goog.history.Html5History.isSupported()){ - this.history_= new goog.history.Html5History();this.history_.setUseFragment(false); -} -else -{ - this.history_=new goog.History(); -} -}; + /** * pass through the fragment for the URL * @@ -51,7 +50,11 @@ mvc.Router.prototype.navigate = function(fragment) { */ mvc.Router.prototype.route = function(route, fn) { if(goog.isString(route)) - route = new RegExp('^' + goog.string.regExpEscape(route).replace(/\\:\w+/g, '(\\w+)').replace(/\\\*/g, '(.*)').replace(/\\\[/g,"(").replace(/\\\]/g,")?") + '$'); + route = new RegExp('^' + goog.string.regExpEscape(route) + .replace(/\\:\w+/g, '(\\w+)') + .replace(/\\\*/g, '(.*)') + .replace(/\\\[/g,"(") + .replace(/\\\]/g,")?") + '$'); this.routes_.push({route: route, callback: fn}); };