backbone.base-router
Advanced tools
Comparing version 0.2.0 to 0.3.0
{ | ||
"name": "backbone.base-router", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"homepage": "https://github.com/jmeas/backbone.base-router", | ||
@@ -5,0 +5,0 @@ "authors": [ |
@@ -1,2 +0,2 @@ | ||
// Backbone.BaseRouter v0.2.0 | ||
// Backbone.BaseRouter v0.3.0 | ||
(function(root, factory) { | ||
@@ -33,5 +33,2 @@ if (typeof define === 'function' && define.amd) { | ||
// The instance of history that this Router uses | ||
history: Backbone.history, | ||
// The single point of entry. This is called whenever a | ||
@@ -58,3 +55,3 @@ // route is matched. The routeData argument contains lots of | ||
var router = this; | ||
this.history.route(route, function(fragment, navOptions) { | ||
Backbone.history.route(route, function(fragment, navOptions) { | ||
var routeParams = router._extractParameters(route, fragment); | ||
@@ -61,0 +58,0 @@ var queryString = routeParams.pop(); |
@@ -1,4 +0,4 @@ | ||
// Backbone.BaseRouter v0.2.0 | ||
// Backbone.BaseRouter v0.3.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({history:a.history,onNavigate:function(){},route:function(a,c){var d=b.isRegExp(a)?a:this._routeToRegExp(a),e={route:d,router:this,linked:c};b.isRegExp(a)||(e.originalRoute=a);var f=this;return this.history.route(d,function(a,b){var c=f._extractParameters(d,a),g=c.pop();b&&(e.navOptions=b),e.query=f._getQueryParameters(g),e.params=f._getNamedParams(d,c),e.uriFragment=a,f.onNavigate(e)}),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=/\((.*?)\)/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}); | ||
//# sourceMappingURL=backbone.base-router.min.js.map |
var Model = Backbone.Model.extend({ | ||
// We're going to override fetch to simply return a promise after a moment | ||
// Because I don't actually want to fetch any data for this example, | ||
// I'm overriding the method to return a Promise that resolves after 1s has passed. | ||
fetch: function() { | ||
@@ -5,0 +6,0 @@ return new Promise(function(resolve) { |
{ | ||
"name": "backbone.base-router", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "A better starting point for a new Backbone Router.", | ||
@@ -5,0 +5,0 @@ "main": "dist/backbone.base-router.js", |
@@ -13,3 +13,3 @@ # backbone.base-router | ||
lets you associate whatever you'd like with a route. It can be a callback if you want, | ||
but it can also be an object, or a string. | ||
but it can also be an object, or even a string. | ||
@@ -27,28 +27,14 @@ Whenever a Route is matched, a single method on the Router is called. This method is passed a single | ||
- [Getting Started](#getting-started) | ||
- [Backwards Compatibility](#backwards-compatibility) | ||
- [History](#history) | ||
- [A Single Point of Entry](#a-single-point-of-entry) | ||
- [The Single Point of Entry](#the-single-point-of-entry) | ||
- [Removed Features](#removed-features) | ||
- [Example Usage](#example-usage) | ||
- [API](#api) | ||
- [history](#history) | ||
- [onNavigate](#onnavigate-routedata-) | ||
- [Removed Features](#removed-features) | ||
- [Accessing the options passed to `route`](#accessing-the-options-passed-to-route) | ||
- [Example Usage](#example-usage) | ||
### Getting Started | ||
#### Backwards Compatibility | ||
#### The Single Point of Entry | ||
This library is backwards compatible with any existing Backbone application. It can even run concurrently | ||
alongside an existing Router. | ||
#### History | ||
Backbone's Router is intimately tied to Backbone.History (more specifically, an instance | ||
of that Class called Backbone.history). With a regular Router you can't specify what instance | ||
of history that you're using, but you can with this one. To use a different instance of History, specify the | ||
`history` property on the Router. | ||
#### A Single Point of Entry | ||
The API for the Base Router is simple: there's a single callback that gets called when a Route is navigated | ||
@@ -59,8 +45,19 @@ to. This callback is a plethora of information you might need, such as parsed query | ||
## API | ||
#### Removed features | ||
### `history` | ||
Backbone.BaseRouter does more for you by doing less. The following features were removed from the router. | ||
The instance of `history` to use in conjunction with this Router. | ||
- A callback, if specified, is not automatically executed | ||
- No routing-related events are fired | ||
The point of removing these features is that it gives you complete control over the Routing | ||
mechanism. It's simple to add them back in. Or you can change them to be exactly how you | ||
want. Or just leave them out. It's entirely up to you. | ||
#### Example Usage | ||
See the [`examples/`](https://github.com/jmeas/backbone.base-router/tree/updates/examples) directory. There are READMEs for each example. | ||
## API | ||
### `onNavigate( routeData )` | ||
@@ -118,36 +115,1 @@ | ||
The URI fragment that was matched. | ||
### Removed features | ||
Backbone.BaseRouter does more for you by doing less. The following features were removed from the router. | ||
- A callback, if specified, is not executed | ||
- Related to the above; the `trigger` option to the `navigate` method no longer exists | ||
- No routing-related events are fired | ||
The point of removing these features is that it gives you complete control over the Routing | ||
mechanism. It's simple to add them back in. Or you can change them to be exactly how you | ||
want. Or just leave them out. It's entirely up to you. | ||
### Accessing the options passed to `route` | ||
As you might already know, you can pass options when you call the `route` method. | ||
```js | ||
myRouter.navigate('some/path', {myOption: true}); | ||
``` | ||
Backbone Routers do not get these options back. They're sent off to History, which does not | ||
return them. Because this library is *just* a Router, it doesn't let you access those options. However, if you want | ||
these options you can use [Backbone.BaseHistory](https://github.com/jmeas/backbone.base-history) in conjunction | ||
with this library. If you use that library you will receive those options in your `routeData` as a property | ||
called `navOptions`. | ||
```js | ||
var BaseHistory = require('BaseHistory'); | ||
myRoute.history = new baseHistory(); | ||
``` | ||
## Example Usage | ||
See the [`examples/`](https://github.com/jmeas/backbone.base-router/tree/updates/examples) directory. There are READMEs for each example. |
@@ -15,5 +15,2 @@ // | ||
// The instance of history that this Router uses | ||
history: Backbone.history, | ||
// The single point of entry. This is called whenever a | ||
@@ -40,3 +37,3 @@ // route is matched. The routeData argument contains lots of | ||
var router = this; | ||
this.history.route(route, function(fragment, navOptions) { | ||
Backbone.history.route(route, function(fragment, navOptions) { | ||
var routeParams = router._extractParameters(route, fragment); | ||
@@ -43,0 +40,0 @@ var queryString = routeParams.pop(); |
@@ -11,16 +11,2 @@ describe('Base Router', function() { | ||
describe('when specifying a different instance of history', function() { | ||
beforeEach(function() { | ||
this.newHistory = { route: this.sinon.stub() }; | ||
this.router.history = this.newHistory; | ||
this.sinon.spy(Backbone.history, 'route'); | ||
this.router.route('what', true); | ||
}); | ||
it('should route on the specified history, not Backbone.history', function() { | ||
expect(Backbone.history.route).to.not.have.been.called; | ||
expect(this.newHistory.route).to.have.been.calledOnce; | ||
}); | ||
}); | ||
describe('when routing to a matched route with pushState', function() { | ||
@@ -27,0 +13,0 @@ beforeEach(function() { |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52461
918
112