backbone-highway
Advanced tools
Comparing version 0.5.0 to 0.5.1
{ | ||
"name": "backbone-highway", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"authors": [ | ||
@@ -5,0 +5,0 @@ "Carl OGREN <rascarlito@gmail.com>" |
@@ -13,3 +13,3 @@ (function () { | ||
console.log('Controller action: 404, path: %s', path); | ||
$('.content').html('<h1>404 ! =(</h1>'); | ||
$('.content').html('404 ! =('); | ||
} | ||
@@ -22,3 +22,3 @@ }); | ||
console.log('Controller action: 403, path: %s', path); | ||
$('.content').html('<h1>403 ! =(</h1>'); | ||
$('.content').html('403 ! =('); | ||
} | ||
@@ -77,6 +77,17 @@ }); | ||
this.route('alias.test', { | ||
path: '/alias/:id', | ||
before: [ | ||
{path: '/users/1234', args: [5432]} | ||
// {name: 'user_show', args: [99]} | ||
], | ||
action: function (id) { | ||
console.log('Alias test: id=%s', id); | ||
} | ||
}); | ||
// Declaring a secured route with a parameter | ||
this.route('user_show', { | ||
path: '/users/:id', | ||
authenticated: true, | ||
// authenticated: true, | ||
action: function (userId) { | ||
@@ -83,0 +94,0 @@ console.log('Controller action: user_show'); |
{ | ||
"name": "backbone-highway", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "Routing Backbone with style \\o/", | ||
@@ -5,0 +5,0 @@ "author": "Carl OGREN <rascarlito@gmail.com>", |
@@ -56,7 +56,14 @@ (function (window, factory) { | ||
// Default options that are extended when the router is started | ||
var re = { | ||
headingSlash: /^(\/|#)/, | ||
trailingSlash: /\/$/ | ||
}; | ||
// -------------------------------- | ||
// **Default options that are extended when the router is started** | ||
// - *@type {Object}* | ||
var defaultOptions = { | ||
// -------------------------------- | ||
// ### Backbone History options | ||
// #### Backbone History options | ||
// Docs: http://backbonejs.org/#History | ||
@@ -67,3 +74,3 @@ | ||
// Root url | ||
// Root url for pushState | ||
root: '', | ||
@@ -77,3 +84,3 @@ | ||
// -------------------------------- | ||
// #### Backbone.Highway specific options | ||
@@ -96,2 +103,3 @@ // Event aggregator used to dispatch triggers | ||
// Local storage key prefix | ||
storePrefix: 'backbone-highway', | ||
@@ -176,5 +184,8 @@ | ||
this.processControllers(self.options.routes.error404, [self.options.pushState ? | ||
window.location.pathname.substring(1) : window.location.hash.substring(1) | ||
]); | ||
this.processControllers({ | ||
name: this.options.routes.error404, | ||
args: [ | ||
Backbone.history.getFragment() | ||
] | ||
}); | ||
} | ||
@@ -296,4 +307,4 @@ else { | ||
// Remove the first slash in the path for the Backbone router | ||
if (def.path && def.path.charAt(0) === '/') { | ||
def.path = def.path.substring(1); | ||
if (def.path) { | ||
def.path = this.stripHeadingSlash(def.path); | ||
} | ||
@@ -312,3 +323,3 @@ | ||
extendedController[name] = { | ||
re: _.isString(def.path) ? Backbone.Router.prototype._routeToRegExp(def.path) : null, | ||
re: _.isString(def.path) ? this.routeToRegExp(def.path) : null, | ||
wrappers: [] | ||
@@ -322,3 +333,3 @@ }; | ||
controllerExtension[name] = function () { | ||
self.processControllers(name, arguments); | ||
self.processControllers({name: name, args: arguments}); | ||
}; | ||
@@ -362,3 +373,3 @@ | ||
// Redirect to login | ||
self.processControllers(self.options.routes.login); | ||
self.processControllers({name: self.options.routes.login}); | ||
} | ||
@@ -372,5 +383,8 @@ else { | ||
// @todo Apply better/finer logic for when the 403 controller should be executed | ||
this.processControllers(self.options.routes.error403, [self.options.pushState ? | ||
window.location.pathname.substring(1) : window.location.hash.substring(1) | ||
]); | ||
this.processControllers({ | ||
name: this.options.routes.error403, | ||
args: [ | ||
Backbone.history.getFragment() | ||
] | ||
}); | ||
} | ||
@@ -381,2 +395,3 @@ return false; | ||
// Check if the route is an alias | ||
// - FIXME Aliasing through the action parameter will probably conflict with before/after triggers | ||
if (_.isString(def.action)) { | ||
@@ -386,3 +401,3 @@ self.options.log('[Backbone.Highway] Caught alias route: "' + currentName + '" >> "' + def.action + '"'); | ||
// Execute alias route | ||
self.processControllers(def.action, args, true); | ||
self.processControllers({name: def.action, args: args}, true); | ||
@@ -397,3 +412,3 @@ return false; | ||
if (!_.isEmpty(def.before)) { | ||
self.processTriggers(def.before); | ||
self.processTriggers(def.before, args); | ||
} | ||
@@ -408,3 +423,3 @@ | ||
if (!_.isEmpty(def.after)) { | ||
self.processTriggers(def.after); | ||
self.processTriggers(def.after, args); | ||
} | ||
@@ -450,3 +465,3 @@ }; | ||
// Transfer route path and remove first slash | ||
path = _.isString(route.path) && route.path.charAt(0) === '/' ? route.path.substring(1) : route.path; | ||
path = _.isString(route.path) && this.stripHeadingSlash(route.path); | ||
@@ -470,5 +485,8 @@ // Transfer args | ||
// Execute 404 controller | ||
this.processControllers(this.options.routes.error404, [this.options.pushState ? | ||
window.location.pathname.substring(1) : window.location.hash.substring(1) | ||
]); | ||
this.processControllers({ | ||
name: this.options.routes.error404, | ||
args: [ | ||
Backbone.history.getFragment() | ||
] | ||
}); | ||
} | ||
@@ -519,3 +537,3 @@ else { | ||
// - @param {Array} **triggers** The list of triggers to process* | ||
processTriggers: function (triggers) { | ||
processTriggers: function (triggers, routeArgs) { | ||
var self = this; | ||
@@ -525,7 +543,7 @@ | ||
_.forEach(triggers, function (trigger) { | ||
self.processTrigger(trigger); | ||
self.processTrigger(trigger, routeArgs); | ||
}); | ||
} | ||
else if (_.isString(triggers) || _.isObject(triggers)) { | ||
this.processTrigger(triggers); | ||
this.processTrigger(triggers, routeArgs); | ||
} | ||
@@ -542,6 +560,8 @@ else { | ||
// - @param {Mixed} **trigger** String or Object describing the trigger* | ||
processTrigger: function (trigger) { | ||
processTrigger: function (trigger, routeArgs) { | ||
if (_.isObject(trigger)) { | ||
// Create a dispatcher format object | ||
var args = [trigger.name]; | ||
// Retrieve the name of the trigger if it's declared using a path | ||
if (trigger.path) { | ||
trigger.name = this.name(trigger.path); | ||
} | ||
@@ -564,18 +584,20 @@ // Check if the trigger is marked for caching | ||
// debugger; | ||
// Wrap the given parameter in an array | ||
if (!_.isUndefined(trigger.args) && !_.isNull(trigger.args) && !_.isArray(trigger.args)) { | ||
trigger.args = [trigger.args]; | ||
} | ||
// Check if the trigger is actually a declared route | ||
if (this.exists({name: trigger.name})) { | ||
this.processControllers(trigger.name, trigger.args || null, true); | ||
return; | ||
if (_.isEmpty(trigger.args)) { | ||
trigger.args = routeArgs; | ||
} | ||
else if (this.exists({path: trigger.path})) { | ||
this.processControllers(this.name(trigger.path), trigger.args || null, true); | ||
} | ||
// Wrap the given parameter in an array | ||
if (!_.isArray(trigger.args)) { | ||
trigger.args = [trigger.args]; | ||
// If the trigger is actually a controller execute it | ||
if (trigger.name && this.exists({name: trigger.name})) { | ||
this.processControllers(trigger, true); | ||
return; | ||
} | ||
// Create a dispatcher format object | ||
var args = [trigger.name]; | ||
// Finish formatting trigger arguments for the dispatcher | ||
@@ -586,3 +608,3 @@ _.forEach(trigger.args, function (arg) { | ||
// Dispatch the event | ||
// Dispatch the event applying arguments | ||
this.dispatcher.trigger.apply(this.dispatcher, args); | ||
@@ -593,3 +615,6 @@ } | ||
if (this.exists({name: trigger})) { | ||
this.processControllers(trigger, null, true); | ||
this.processControllers({ | ||
name: trigger, | ||
args: routeArgs | ||
}, true); | ||
} | ||
@@ -612,4 +637,6 @@ else { | ||
// - @param {Array} **args** JavaScript arguments array* | ||
processControllers: function (name, args, trigger) { | ||
var self = this; | ||
processControllers: function (def, trigger) { | ||
var self = this, | ||
name = def.name, | ||
args = def.args; | ||
@@ -619,13 +646,23 @@ // Do not interpret control as a trigger by default | ||
// Lets not pass [undefined] or [null] as arguments to the controllers | ||
if (_.isUndefined(args) || _.isNull(args)) { | ||
args = []; | ||
} | ||
// Ensure args is an array or an arguments object | ||
else if (!_.isObject && !_.isArray(args)) { | ||
args = [args]; | ||
} | ||
// Check if the given controller actually exists | ||
if (extendedController[name]) { | ||
// Extract parameters from path if possible | ||
if (def.path) { | ||
args = this.extractParameters(name, def.path); | ||
// Backbone gives [null] for routes without arguments | ||
// so if there is not more than one argument use the passed arguments instead | ||
if (args.length === 1 && args[0] === null) { | ||
args = def.args; | ||
} | ||
} | ||
// Lets not pass [undefined] or [null] as arguments to the controllers | ||
if (_.isUndefined(args) || _.isNull(args)) { | ||
args = []; | ||
} | ||
// Ensure args is an array or an arguments object | ||
else if (!_.isObject(args) && !_.isArray(args)) { | ||
args = [args]; | ||
} | ||
// Loop through each defined route controller | ||
@@ -698,5 +735,3 @@ _.forEach(extendedController[name].wrappers, function (callback) { | ||
// Remove first slash | ||
if (path.indexOf('/') === 0) { | ||
path = path.substring(1); | ||
} | ||
path = this.stripHeadingSlash(path); | ||
@@ -748,2 +783,3 @@ // Loop through all the controllers | ||
// Inject passed arguments | ||
// - FIXME Replace with a regex : var re = /(?:.*)?((:[^\/]+)+)(?:.*)?/g ? | ||
return _.map(path.split('/'), function (part) { | ||
@@ -762,3 +798,3 @@ if (part.charAt(0) === ':') { | ||
// Remove trailing slash | ||
.replace(/\/$/, ''); | ||
.replace(re.trailingSlash, ''); | ||
}, | ||
@@ -771,7 +807,4 @@ | ||
// Retrieve current path | ||
var path = this.options.pushState ? window.location.pathname : window.location.hash; | ||
var path = Backbone.history.getFragment(); | ||
// Remove first / or # | ||
path = path.substring(1); | ||
this.options.log('[Backbone.Highway] Storing current path: ' + path); | ||
@@ -805,2 +838,26 @@ | ||
// **Remove heading slash or pound sign from a path, if any** | ||
stripHeadingSlash: function (path) { | ||
return path.replace(re.headingSlash, ''); | ||
}, | ||
// -------------------------------- | ||
// **Extract parameters from the path of a route** | ||
extractParameters: function (name, path) { | ||
return Backbone.Router.prototype._extractParameters( | ||
extendedController[name].re, | ||
this.stripHeadingSlash(path) | ||
); | ||
}, | ||
// -------------------------------- | ||
// **Transform a route path to a regular expression** | ||
routeToRegExp: function (path) { | ||
return Backbone.Router.prototype._routeToRegExp(path); | ||
}, | ||
// -------------------------------- | ||
// **Original Backbone.Router Methods** | ||
@@ -807,0 +864,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
564198
38
1998