Skip to content

Commit 278f6b7

Browse files
authored
Merge pull request #8 from mhulse/314-fix-set-route
Fixed setRoute (flatiron#314)
2 parents d737d8d + f5c8e01 commit 278f6b7

File tree

6 files changed

+202
-10
lines changed

6 files changed

+202
-10
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -939,7 +939,16 @@ Remove a segment from the current route.
939939
* `value` {String} - The new value to assign the the position indicated by the
940940
first parameter.
941941

942-
Set a segment of the current route.
942+
Replace a segment of the current route.
943+
944+
### setRoute(start, length, value)
945+
946+
* `start` {Number} - The position at which to start removing items.
947+
* `length` {Number} - The number of items to remove from the route.
948+
* `value` {String} - The new value to assign the the position indicated by the
949+
first parameter.
950+
951+
Remove and set a segment of the current route.
943952

944953
# Frequently Asked Questions
945954

build/tarantino.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,7 @@ var listener = {
938938
}
939939

940940
if (this.history === true) {
941+
s = ((s.indexOf('/') === 0) ? s : '/' + s);
941942
window.history.pushState({}, document.title, s);
942943
// Fire an onpopstate event manually since pushing does not obviously
943944
// trigger the pop event.
@@ -1040,12 +1041,23 @@ Router$$1.prototype.explode = function () {
10401041
Router$$1.prototype.setRoute = function (i, v, val) {
10411042
var url = this.explode();
10421043

1043-
if (typeof i === 'number' && typeof v === 'string') {
1044-
url[i] = v;
1044+
// Remove and insert:
1045+
if (typeof i === 'number' && typeof v === 'number' && typeof val === 'string') {
1046+
if (i < url.length) {
1047+
url.splice(i, v, val);
1048+
}
10451049
}
1046-
else if (typeof val === 'string') {
1047-
url.splice(i, v, s);
1050+
// Remove:
1051+
else if (typeof i === 'number' && typeof v === 'number') {
1052+
if (i < url.length) {
1053+
url.splice(i, v);
1054+
}
1055+
}
1056+
// Replace:
1057+
else if (typeof i === 'number' && typeof v === 'string') {
1058+
url[i] = v;
10481059
}
1060+
// Do nothing:
10491061
else {
10501062
url = [i];
10511063
}

build/tarantino.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/browser.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ var listener = {
114114
}
115115

116116
if (this.history === true) {
117+
s = ((s.indexOf('/') === 0) ? s : '/' + s);
117118
window.history.pushState({}, document.title, s);
118119
// Fire an onpopstate event manually since pushing does not obviously
119120
// trigger the pop event.
@@ -216,12 +217,23 @@ Router.prototype.explode = function () {
216217
Router.prototype.setRoute = function (i, v, val) {
217218
var url = this.explode();
218219

219-
if (typeof i === 'number' && typeof v === 'string') {
220-
url[i] = v;
220+
// Remove and insert:
221+
if (typeof i === 'number' && typeof v === 'number' && typeof val === 'string') {
222+
if (i < url.length) {
223+
url.splice(i, v, val);
224+
}
221225
}
222-
else if (typeof val === 'string') {
223-
url.splice(i, v, s);
226+
// Remove:
227+
else if (typeof i === 'number' && typeof v === 'number') {
228+
if (i < url.length) {
229+
url.splice(i, v);
230+
}
231+
}
232+
// Replace:
233+
else if (typeof i === 'number' && typeof v === 'string') {
234+
url[i] = v;
224235
}
236+
// Do nothing:
225237
else {
226238
url = [i];
227239
}

test/browser/html5-routes-test.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,96 @@ createTest('setRoute with a single parameter should change location correctly',
642642
}, 14)
643643
});
644644

645+
createTest('setRoute: replace a specific segment of the current route and change location correctly', {
646+
'/authors/12/book/23': {
647+
on: function() {
648+
if (!browser_history_support) {
649+
shared.fired.push(location.hash.replace(/^#/, ''));
650+
} else {
651+
shared.fired.push(location.pathname);
652+
}
653+
}
654+
},
655+
'/authors/12/video/23': {
656+
on: function() {
657+
if (!browser_history_support) {
658+
shared.fired.push(location.hash.replace(/^#/, ''));
659+
} else {
660+
shared.fired.push(location.pathname);
661+
}
662+
}
663+
}
664+
}, function(assert) {
665+
var self = this;
666+
this.navigate('/authors/12/book/23', function() {
667+
this.router.setRoute(2, 'video');
668+
setTimeout(function() {
669+
assert.deepEqual(shared.fired, ['/authors/12/book/23', '/authors/12/video/23']);
670+
self.finish();
671+
}, 14);
672+
});
673+
});
674+
675+
createTest('setRoute: remove segments of the current route and change location correctly', {
676+
'/authors/12/book/23': {
677+
on: function() {
678+
if (!browser_history_support) {
679+
shared.fired.push(location.hash.replace(/^#/, ''));
680+
} else {
681+
shared.fired.push(location.pathname);
682+
}
683+
}
684+
},
685+
'/authors/12': {
686+
on: function() {
687+
if (!browser_history_support) {
688+
shared.fired.push(location.hash.replace(/^#/, ''));
689+
} else {
690+
shared.fired.push(location.pathname);
691+
}
692+
}
693+
}
694+
}, function(assert) {
695+
var self = this;
696+
this.navigate('/authors/12/book/23', function() {
697+
this.router.setRoute(2, 2);
698+
setTimeout(function() {
699+
assert.deepEqual(shared.fired, ['/authors/12/book/23', '/authors/12']);
700+
self.finish();
701+
}, 14);
702+
});
703+
});
704+
705+
createTest('setRoute: insert and replace a segment into the current route and change location correctly', {
706+
'/authors/12/book/23': {
707+
on: function() {
708+
if (!browser_history_support) {
709+
shared.fired.push(location.hash.replace(/^#/, ''));
710+
} else {
711+
shared.fired.push(location.pathname);
712+
}
713+
}
714+
},
715+
'/authors/12/awesome': {
716+
on: function() {
717+
if (!browser_history_support) {
718+
shared.fired.push(location.hash.replace(/^#/, ''));
719+
} else {
720+
shared.fired.push(location.pathname);
721+
}
722+
}
723+
}
724+
}, function(assert) {
725+
var self = this;
726+
this.navigate('/authors/12/book/23', function() {
727+
this.router.setRoute(2, 2, 'awesome');
728+
setTimeout(function() {
729+
assert.deepEqual(shared.fired, ['/authors/12/book/23', '/authors/12/awesome']);
730+
self.finish();
731+
}, 14);
732+
});
733+
});
734+
645735
createTest('route should accept _ and . within parameters', {
646736
'/:a': {
647737
on: function root() {

test/browser/routes-test.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,75 @@ createTest('setRoute with a single parameter should change location correctly',
630630
}, 14)
631631
});
632632

633+
createTest('setRoute: replace a specific segment of the current route and change location correctly', {
634+
'/authors/12/book/23': {
635+
on: function() {
636+
shared.fired.push(window.location.hash);
637+
}
638+
},
639+
'/authors/12/video/23': {
640+
on: function() {
641+
shared.fired.push(window.location.hash);
642+
}
643+
}
644+
}, function(assert) {
645+
var self = this;
646+
shared.fired = [];
647+
this.navigate('/authors/12/book/23', function() {
648+
this.router.setRoute(2, 'video');
649+
setTimeout(function() {
650+
assert.deepEqual(shared.fired, ['#/authors/12/book/23', '#/authors/12/video/23']);
651+
self.finish();
652+
}, 14);
653+
});
654+
});
655+
656+
createTest('setRoute: remove segments of the current route and change location correctly', {
657+
'/authors/12/book/23': {
658+
on: function() {
659+
shared.fired.push(window.location.hash);
660+
}
661+
},
662+
'/authors/12': {
663+
on: function() {
664+
shared.fired.push(window.location.hash);
665+
}
666+
}
667+
}, function(assert) {
668+
var self = this;
669+
shared.fired = [];
670+
this.navigate('/authors/12/book/23', function() {
671+
this.router.setRoute(2, 2);
672+
setTimeout(function() {
673+
assert.deepEqual(shared.fired, ['#/authors/12/book/23', '#/authors/12']);
674+
self.finish();
675+
}, 14);
676+
});
677+
});
678+
679+
createTest('setRoute: insert and replace a segment into the current route and change location correctly', {
680+
'/authors/12/book/23': {
681+
on: function() {
682+
shared.fired.push(window.location.hash);
683+
}
684+
},
685+
'/authors/12/awesome': {
686+
on: function() {
687+
shared.fired.push(window.location.hash);
688+
}
689+
}
690+
}, function(assert) {
691+
var self = this;
692+
shared.fired = [];
693+
this.navigate('/authors/12/book/23', function() {
694+
this.router.setRoute(2, 2, 'awesome');
695+
setTimeout(function() {
696+
assert.deepEqual(shared.fired, ['#/authors/12/book/23', '#/authors/12/awesome']);
697+
self.finish();
698+
}, 14);
699+
});
700+
});
701+
633702
createTest('route should accept _ and . within parameters', {
634703
'/:a': {
635704
on: function root() {

0 commit comments

Comments
 (0)