Skip to content

Commit

Permalink
Adding parameter arguments to router callbacks, fixes #16083
Browse files Browse the repository at this point in the history
  • Loading branch information
brianarn committed Apr 4, 2013
1 parent d24151d commit 5c15e00
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
16 changes: 14 additions & 2 deletions router/RouterBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ define([
// no clean way to expose this on the prototype since it's for the
// internal router objects.
function fireRoute(params, currentPath, newPath){
var queue, isStopped, isPrevented, eventObj, i, l;
var queue, isStopped, isPrevented, eventObj, callbackArgs, i, l;

queue = this.callbackQueue;
isStopped = false;
Expand All @@ -33,9 +33,19 @@ define([
params: params
};

callbackArgs = [eventObj];

if(params instanceof Array){
callbackArgs = callbackArgs.concat(params);
}else{
for(var key in params){
callbackArgs.push(params[key]);
}
}

for(i=0, l=queue.length; i<l; ++i){
if(!isStopped){
queue[i](eventObj);
queue[i].apply(null, callbackArgs);
}
}

Expand Down Expand Up @@ -171,6 +181,8 @@ define([

var applyChange;

if(typeof path !== "string"){return false;}

path = trim(path);
applyChange = this._handlePathChange(path);

Expand Down
68 changes: 53 additions & 15 deletions tests/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,12 +132,12 @@ define([

router.go("");

handle = router.register("/checkEventObject/:foo", function(e){
oldPath = e.oldPath;
newPath = e.newPath;
params = e.params;
stopImmediatePropagation = typeof e.stopImmediatePropagation;
preventDefault = typeof e.preventDefault;
handle = router.register("/checkEventObject/:foo", function(event){
oldPath = event.oldPath;
newPath = event.newPath;
params = event.params;
stopImmediatePropagation = typeof event.stopImmediatePropagation;
preventDefault = typeof event.preventDefault;
});

router.go("/checkEventObject/bar");
Expand All @@ -154,13 +154,51 @@ define([
handle.remove();
}
},
{
name: "Checking extra arguments - string route",
runTest: function(t){
var a, b;

handle = router.register("/stringtest/:applied/:arg", function(event, applied, arg){
a = applied;
b = arg;
});

router.go("/stringtest/extra/args");

t.t(a === "extra", "a should have been 'extra', was " + a);
t.t(b === "args", "b should have been 'args', was " + b);
},
tearDown: function(){
handle.remove();
}
},
{
name: "Checking extra arguments - regex route",
runTest: function(t){
var a, b;

handle = router.register(/\/regextest\/(\w+)\/(\w+)/, function(event, applied, arg){
a = applied;
b = arg;
});

router.go("/regextest/extra/args");

t.t(a === "extra", "a should have been 'extra', was " + a);
t.t(b === "args", "b should have been 'args', was " + b);
},
tearDown: function(){
handle.remove();
}
},
{
name: "Registering long routes with placeholders",
runTest: function(t){
var testObject;

handle = router.register("/path/:to/:some/:long/*thing", function(e){
testObject = e.params;
handle = router.register("/path/:to/:some/:long/*thing", function(event){
testObject = event.params;
});

router.go("/path/to/some/long/thing/this/is/in/splat");
Expand Down Expand Up @@ -190,8 +228,8 @@ define([
runTest: function(t){
var testObject;

handle = router.register(/^\/path\/(\w+)\/(\d+)$/, function(e){
testObject = e.params;
handle = router.register(/^\/path\/(\w+)\/(\d+)$/, function(event){
testObject = event.params;
});

router.go("/path/abcdef/1234");
Expand Down Expand Up @@ -265,8 +303,8 @@ define([
handle.push(router.register("/stopImmediatePropagation", function(){ test += "A"; }));
handle.push(router.register("/stopImmediatePropagation", function(){ test += "B"; }));

handle.push(router.register("/stopImmediatePropagation", function(e){
e.stopImmediatePropagation();
handle.push(router.register("/stopImmediatePropagation", function(event){
event.stopImmediatePropagation();
test += "C";
}));

Expand All @@ -290,8 +328,8 @@ define([

t.t(hash() === "", "hash should be empty");

handle.push(router.register("/preventDefault", function(e){
e.preventDefault();
handle.push(router.register("/preventDefault", function(event){
event.preventDefault();
}));

goResult = router.go("/preventDefault");
Expand All @@ -304,7 +342,7 @@ define([
t.t(hash() === "/someOtherPath", "hash should be '/someOtherPath'");
t.t(goResult === true, "goResult should be true");

handle.push(router.register("/allowDefault", function(e){
handle.push(router.register("/allowDefault", function(event){
console.log("Doing something here without explicitly stopping");
}));
},
Expand Down

0 comments on commit 5c15e00

Please sign in to comment.