backbone.base-router
Advanced tools
Comparing version
{ | ||
"name": "backbone.base-router", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"homepage": "https://github.com/jmeas/backbone.base-router", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -1,2 +0,2 @@ | ||
// Backbone.BaseRouter v0.3.0 | ||
// Backbone.BaseRouter v0.4.0 | ||
(function(root, factory) { | ||
@@ -23,11 +23,10 @@ if (typeof define === 'function' && define.amd) { | ||
'use strict'; | ||
// Copied over from Backbone, because it doesn't expose them. | ||
var optionalParam = /\((.*?)\)/g; | ||
var namedParam = /(\(\?)?:\w+/g; | ||
var splatParam = /\*\w+/g; | ||
var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g; | ||
var namedParam = /(\(\?)?:\w+/g; | ||
Backbone.BaseRouter = Backbone.Router.extend({ | ||
constructor: function() { | ||
this.routeParams = {}; | ||
Backbone.Router.constructor.apply(this, arguments); | ||
}, | ||
@@ -40,4 +39,14 @@ // The single point of entry. This is called whenever a | ||
route: function(origRoute, linked) { | ||
var route = _.isRegExp(origRoute) ? origRoute : this._routeToRegExp(origRoute); | ||
var route, routeStr; | ||
if (_.isRegExp(origRoute)) { | ||
route = origRoute; | ||
routeStr = origRoute.toString(); | ||
} else { | ||
route = this._routeToRegExp(origRoute); | ||
routeStr = origRoute; | ||
} | ||
this.routeParams[origRoute] = this._extractRouteParams(routeStr); | ||
// Begin setting up our routeData, | ||
@@ -52,3 +61,5 @@ // based on what we already know. | ||
// Only attach the originalRoute to routeData if it isn't a RegExp. | ||
if (!_.isRegExp(origRoute)) { routeData.originalRoute = origRoute; } | ||
if (!_.isRegExp(origRoute)) { | ||
routeData.originalRoute = origRoute; | ||
} | ||
@@ -64,3 +75,3 @@ // Register a callback with history | ||
routeData.query = router._getQueryParameters(queryString); | ||
routeData.params = router._getNamedParams(route, routeParams); | ||
routeData.params = router._getNamedParams(routeStr, routeParams); | ||
routeData.uriFragment = fragment; | ||
@@ -74,16 +85,10 @@ | ||
_routeToRegExp: function(route) { | ||
this.routeParams = this.routeParams || {}; | ||
_extractRouteParams: function(route) { | ||
var namedParams = []; | ||
var namedParams = []; | ||
var newRoute = route.replace(escapeRegExp, '\\$&') | ||
.replace(optionalParam, '(?:$1)?') | ||
.replace(namedParam, function(match, optional) { | ||
namedParams.push(match.substr(1)); | ||
return optional ? match : '([^/?]+)'; | ||
}) | ||
.replace(splatParam, '([^?]*?)'); | ||
var regexStr = '^' + newRoute + '(?:\\?([\\s\\S]*))?$'; | ||
this.routeParams[regexStr] = namedParams; | ||
return new RegExp(regexStr); | ||
route.replace(namedParam, function(match, optional) { | ||
namedParams.push(match.substr(1)); | ||
}); | ||
return namedParams; | ||
}, | ||
@@ -96,26 +101,27 @@ | ||
if (!queryString) { return {}; } | ||
var match, | ||
pl = /\+/g, // Regex for replacing addition symbol with a space | ||
search = /([^&=]+)=?([^&]*)/g, | ||
decode = function (s) { return decodeURIComponent(s.replace(pl, ' ')); }, | ||
query = queryString; | ||
var match; | ||
var search = /([^&=]+)=?([^&]*)/g; | ||
var urlParams = {}; | ||
while (match = search.exec(query)) { | ||
urlParams[decode(match[1])] = decode(match[2]); | ||
while (match = search.exec(queryString)) { | ||
urlParams[this._decodeParams(match[1])] = this._decodeParams(match[2]); | ||
} | ||
return urlParams; | ||
}, | ||
_decodeParams: function (queryString) { | ||
// Replace addition symbol with a space | ||
return decodeURIComponent(queryString.replace(/\+/g, ' ')); | ||
}, | ||
// Returns the named parameters of the route | ||
_getNamedParams: function(route, routeParams) { | ||
if (routeParams.length === 0) { return {}; } | ||
var routeString = route.toString(); | ||
routeString = routeString.substr(1, routeString.length - 2); | ||
var routeArr = this.routeParams[routeString]; | ||
var paramObj = {}; | ||
_.each(routeArr, function(arrVal, i) { | ||
paramObj[arrVal] = routeParams[i]; | ||
}); | ||
return paramObj; | ||
if (!routeParams.length) { return {}; } | ||
var routeKeys = this.routeParams[route]; | ||
var routeValues = routeParams.slice(0, routeKeys.length); | ||
return _.object(_.zip(routeKeys, routeValues)); | ||
} | ||
@@ -122,0 +128,0 @@ }); |
@@ -1,4 +0,4 @@ | ||
// Backbone.BaseRouter v0.3.0 | ||
// Backbone.BaseRouter v0.4.0 | ||
!function(a,b){if("function"==typeof define&&define.amd)define(["backbone","underscore"],function(a,c){return b(a,c)});else if("undefined"!=typeof exports){var c=require("backbone"),d=require("underscore");module.exports=b(c,d)}else b(a.Backbone,a._)}(this,function(a,b){"use strict";var c=/\((.*?)\)/g,d=/(\(\?)?:\w+/g,e=/\*\w+/g,f=/[\-{}\[\]+?.,\\\^$|#\s]/g;return a.BaseRouter=a.Router.extend({onNavigate:function(){},route:function(c,d){var e=b.isRegExp(c)?c:this._routeToRegExp(c),f={route:e,router:this,linked:d};b.isRegExp(c)||(f.originalRoute=c);var g=this;return a.history.route(e,function(a,b){var c=g._extractParameters(e,a),d=c.pop();b&&(f.navOptions=b),f.query=g._getQueryParameters(d),f.params=g._getNamedParams(e,c),f.uriFragment=a,g.onNavigate(f)}),this},_routeToRegExp:function(a){this.routeParams=this.routeParams||{};var b=[],g=a.replace(f,"\\$&").replace(c,"(?:$1)?").replace(d,function(a,c){return b.push(a.substr(1)),c?a:"([^/?]+)"}).replace(e,"([^?]*?)"),h="^"+g+"(?:\\?([\\s\\S]*))?$";return this.routeParams[h]=b,new RegExp(h)},_getQueryParameters:function(a){if(!a)return{};for(var b,c=/\+/g,d=/([^&=]+)=?([^&]*)/g,e=function(a){return decodeURIComponent(a.replace(c," "))},f=a,g={};b=d.exec(f);)g[e(b[1])]=e(b[2]);return g},_getNamedParams:function(a,c){if(0===c.length)return{};var d=a.toString();d=d.substr(1,d.length-2);var e=this.routeParams[d],f={};return b.each(e,function(a,b){f[a]=c[b]}),f}}),a.BaseRouter}); | ||
!function(a,b){if("function"==typeof define&&define.amd)define(["backbone","underscore"],function(a,c){return b(a,c)});else if("undefined"!=typeof exports){var c=require("backbone"),d=require("underscore");module.exports=b(c,d)}else b(a.Backbone,a._)}(this,function(a,b){"use strict";var c=/(\(\?)?:\w+/g;return a.BaseRouter=a.Router.extend({constructor:function(){this.routeParams={},a.Router.constructor.apply(this,arguments)},onNavigate:function(){},route:function(c,d){var e,f;b.isRegExp(c)?(e=c,f=c.toString()):(e=this._routeToRegExp(c),f=c),this.routeParams[c]=this._extractRouteParams(f);var g={route:e,router:this,linked:d};b.isRegExp(c)||(g.originalRoute=c);var h=this;return a.history.route(e,function(a,b){var c=h._extractParameters(e,a),d=c.pop();b&&(g.navOptions=b),g.query=h._getQueryParameters(d),g.params=h._getNamedParams(f,c),g.uriFragment=a,h.onNavigate(g)}),this},_extractRouteParams:function(a){var b=[];return a.replace(c,function(a){b.push(a.substr(1))}),b},_getQueryParameters:function(a){if(!a)return{};for(var b,c=/([^&=]+)=?([^&]*)/g,d={};b=c.exec(a);)d[this._decodeParams(b[1])]=this._decodeParams(b[2]);return d},_decodeParams:function(a){return decodeURIComponent(a.replace(/\+/g," "))},_getNamedParams:function(a,c){if(!c.length)return{};var d=this.routeParams[a],e=c.slice(0,d.length);return b.object(b.zip(d,e))}}),a.BaseRouter}); | ||
//# sourceMappingURL=backbone.base-router.min.js.map |
{ | ||
"name": "backbone.base-router", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "A better starting point for a new Backbone Router.", | ||
@@ -5,0 +5,0 @@ "main": "dist/backbone.base-router.js", |
@@ -5,11 +5,10 @@ // | ||
'use strict'; | ||
// Copied over from Backbone, because it doesn't expose them. | ||
var optionalParam = /\((.*?)\)/g; | ||
var namedParam = /(\(\?)?:\w+/g; | ||
var splatParam = /\*\w+/g; | ||
var escapeRegExp = /[\-{}\[\]+?.,\\\^$|#\s]/g; | ||
var namedParam = /(\(\?)?:\w+/g; | ||
Backbone.BaseRouter = Backbone.Router.extend({ | ||
constructor: function() { | ||
this.routeParams = {}; | ||
Backbone.Router.constructor.apply(this, arguments); | ||
}, | ||
@@ -22,4 +21,14 @@ // The single point of entry. This is called whenever a | ||
route: function(origRoute, linked) { | ||
var route = _.isRegExp(origRoute) ? origRoute : this._routeToRegExp(origRoute); | ||
var route, routeStr; | ||
if (_.isRegExp(origRoute)) { | ||
route = origRoute; | ||
routeStr = origRoute.toString(); | ||
} else { | ||
route = this._routeToRegExp(origRoute); | ||
routeStr = origRoute; | ||
} | ||
this.routeParams[origRoute] = this._extractRouteParams(routeStr); | ||
// Begin setting up our routeData, | ||
@@ -34,3 +43,5 @@ // based on what we already know. | ||
// Only attach the originalRoute to routeData if it isn't a RegExp. | ||
if (!_.isRegExp(origRoute)) { routeData.originalRoute = origRoute; } | ||
if (!_.isRegExp(origRoute)) { | ||
routeData.originalRoute = origRoute; | ||
} | ||
@@ -46,3 +57,3 @@ // Register a callback with history | ||
routeData.query = router._getQueryParameters(queryString); | ||
routeData.params = router._getNamedParams(route, routeParams); | ||
routeData.params = router._getNamedParams(routeStr, routeParams); | ||
routeData.uriFragment = fragment; | ||
@@ -56,16 +67,10 @@ | ||
_routeToRegExp: function(route) { | ||
this.routeParams = this.routeParams || {}; | ||
_extractRouteParams: function(route) { | ||
var namedParams = []; | ||
var namedParams = []; | ||
var newRoute = route.replace(escapeRegExp, '\\$&') | ||
.replace(optionalParam, '(?:$1)?') | ||
.replace(namedParam, function(match, optional) { | ||
namedParams.push(match.substr(1)); | ||
return optional ? match : '([^/?]+)'; | ||
}) | ||
.replace(splatParam, '([^?]*?)'); | ||
var regexStr = '^' + newRoute + '(?:\\?([\\s\\S]*))?$'; | ||
this.routeParams[regexStr] = namedParams; | ||
return new RegExp(regexStr); | ||
route.replace(namedParam, function(match, optional) { | ||
namedParams.push(match.substr(1)); | ||
}); | ||
return namedParams; | ||
}, | ||
@@ -78,27 +83,28 @@ | ||
if (!queryString) { return {}; } | ||
var match, | ||
pl = /\+/g, // Regex for replacing addition symbol with a space | ||
search = /([^&=]+)=?([^&]*)/g, | ||
decode = function (s) { return decodeURIComponent(s.replace(pl, ' ')); }, | ||
query = queryString; | ||
var match; | ||
var search = /([^&=]+)=?([^&]*)/g; | ||
var urlParams = {}; | ||
while (match = search.exec(query)) { | ||
urlParams[decode(match[1])] = decode(match[2]); | ||
while (match = search.exec(queryString)) { | ||
urlParams[this._decodeParams(match[1])] = this._decodeParams(match[2]); | ||
} | ||
return urlParams; | ||
}, | ||
_decodeParams: function (queryString) { | ||
// Replace addition symbol with a space | ||
return decodeURIComponent(queryString.replace(/\+/g, ' ')); | ||
}, | ||
// Returns the named parameters of the route | ||
_getNamedParams: function(route, routeParams) { | ||
if (routeParams.length === 0) { return {}; } | ||
var routeString = route.toString(); | ||
routeString = routeString.substr(1, routeString.length - 2); | ||
var routeArr = this.routeParams[routeString]; | ||
var paramObj = {}; | ||
_.each(routeArr, function(arrVal, i) { | ||
paramObj[arrVal] = routeParams[i]; | ||
}); | ||
return paramObj; | ||
if (!routeParams.length) { return {}; } | ||
var routeKeys = this.routeParams[route]; | ||
var routeValues = routeParams.slice(0, routeKeys.length); | ||
return _.object(_.zip(routeKeys, routeValues)); | ||
} | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
25
4.17%51915
-1.04%916
-0.22%