Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add remove/replace functionality #258

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion lib/sammy.js
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,57 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {
Sammy.log.apply(Sammy, Array.prototype.concat.apply([this.element_selector],arguments));
},

// `replace()` replaces the verb/path combination in the list of routes
//
// ### Arguments
//
// * `verb` A String in the set of ROUTE_VERBS or 'any'. 'any' will add routes for each
// of the ROUTE_VERBS.
// * `path` A Regexp or a String representing the path to match to invoke this verb.
//
replace: function(verb, path) {
var app = this, param_names = [], add_route, path_match, callback = Array.prototype.slice.call(arguments,2);

// if the method signature is just (path, callback)
// assume the verb is 'any'
if (callback.length === 0 && _isFunction(path)) {
callback = path;
path = verb;
verb = 'any';
}

app.remove(verb, path);
app.route(verb, path, callback);
},

// `remove()` removes the verb/path combination from the list of routes
//
// ### Arguments
//
// * `verb` A String in the set of ROUTE_VERBS or 'any'. 'any' will add routes for each
// of the ROUTE_VERBS.
// * `path` A Regexp or a String representing the path to match to invoke this verb.
//
remove: function(verb, path) {
var app = this, remove_route;

verb = verb.toLowerCase(); // ensure verb is lower case

remove_route = function(with_verb) {
var route;
route = app.lookupRoute(with_verb, path, true);
if (route && app.routablePath(path).match(route.path)) {
app.routes[with_verb].splice(route.routeIndex, 1);
}
}

if (verb === 'any') {
$.each(this.ROUTE_VERBS, function(i, v) { remove_route(v); });
} else {
remove_route(verb);
}
},

// `route()` is the main method for defining routes within an application.
// For great detail on routes, check out:
// [http://sammyjs.org/docs/routes](http://sammyjs.org/docs/routes)
Expand Down Expand Up @@ -1065,14 +1115,20 @@ $.extend(Sammy.DefaultLocationProxy.prototype , {

// Given a verb and a String path, will return either a route object or false
// if a matching route can be found within the current defined set.
lookupRoute: function(verb, path) {
lookupRoute: function(verb, path, includeIndex) {
var app = this, routed = false, i = 0, l, route;
if (typeof this.routes[verb] != 'undefined') {
l = this.routes[verb].length;
for (; i < l; i++) {
route = this.routes[verb][i];
if (app.routablePath(path).match(route.path)) {
routed = route;

// capture the routeIndex in the verb routes for easier removal
if (includeIndex) {
routed.routeIndex = i;
}

break;
}
}
Expand Down