Skip to content

Commit

Permalink
router improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysbrettbowen committed Apr 2, 2012
1 parent b647718 commit 35ddbad
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -246,6 +246,7 @@ you can then register your object with the mediator and the messages that you ma
- schema is now an object - schema is now an object
- mvc.Control events can now be given a priority to order them - mvc.Control events can now be given a priority to order them
- more tests - more tests
- router uses HTML5 where possible and you can pass in parameters to hide the fragment


#### v0.8 #### #### v0.8 ####


Expand Down
33 changes: 18 additions & 15 deletions router.js
Expand Up @@ -10,29 +10,28 @@ goog.provide('mvc.Router');
goog.require('goog.array'); goog.require('goog.array');
goog.require('goog.events'); goog.require('goog.events');
goog.require('goog.History'); goog.require('goog.History');
goog.require('goog.history.Html5History');


/** /**
* @constructor * @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() { mvc.Router = function(opt_noFragment, opt_blankPage) {
this.checkhistorysupport(); 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, goog.events.listen(this.history_, goog.history.EventType.NAVIGATE,
this.onChange_, false, this); this.onChange_, false, this);
this.history_.setEnabled(true); this.history_.setEnabled(true);
this.routes_ = []; 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 * pass through the fragment for the URL
* *
Expand All @@ -51,7 +50,11 @@ mvc.Router.prototype.navigate = function(fragment) {
*/ */
mvc.Router.prototype.route = function(route, fn) { mvc.Router.prototype.route = function(route, fn) {
if(goog.isString(route)) 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}); this.routes_.push({route: route, callback: fn});
}; };


Expand Down

0 comments on commit 35ddbad

Please sign in to comment.