Skip to content

Commit

Permalink
Merge pull request #8 from mhulse/314-fix-set-route
Browse files Browse the repository at this point in the history
Fixed setRoute (flatiron#314)
  • Loading branch information
SpaceK33z authored Feb 1, 2017
2 parents d737d8d + f5c8e01 commit 278f6b7
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 10 deletions.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,16 @@ Remove a segment from the current route.
* `value` {String} - The new value to assign the the position indicated by the
first parameter.

Set a segment of the current route.
Replace a segment of the current route.

### setRoute(start, length, value)

* `start` {Number} - The position at which to start removing items.
* `length` {Number} - The number of items to remove from the route.
* `value` {String} - The new value to assign the the position indicated by the
first parameter.

Remove and set a segment of the current route.

# Frequently Asked Questions

Expand Down
20 changes: 16 additions & 4 deletions build/tarantino.js
Original file line number Diff line number Diff line change
Expand Up @@ -938,6 +938,7 @@ var listener = {
}

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

if (typeof i === 'number' && typeof v === 'string') {
url[i] = v;
// Remove and insert:
if (typeof i === 'number' && typeof v === 'number' && typeof val === 'string') {
if (i < url.length) {
url.splice(i, v, val);
}
}
else if (typeof val === 'string') {
url.splice(i, v, s);
// Remove:
else if (typeof i === 'number' && typeof v === 'number') {
if (i < url.length) {
url.splice(i, v);
}
}
// Replace:
else if (typeof i === 'number' && typeof v === 'string') {
url[i] = v;
}
// Do nothing:
else {
url = [i];
}
Expand Down
2 changes: 1 addition & 1 deletion build/tarantino.min.js

Large diffs are not rendered by default.

20 changes: 16 additions & 4 deletions lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ var listener = {
}

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

if (typeof i === 'number' && typeof v === 'string') {
url[i] = v;
// Remove and insert:
if (typeof i === 'number' && typeof v === 'number' && typeof val === 'string') {
if (i < url.length) {
url.splice(i, v, val);
}
}
else if (typeof val === 'string') {
url.splice(i, v, s);
// Remove:
else if (typeof i === 'number' && typeof v === 'number') {
if (i < url.length) {
url.splice(i, v);
}
}
// Replace:
else if (typeof i === 'number' && typeof v === 'string') {
url[i] = v;
}
// Do nothing:
else {
url = [i];
}
Expand Down
90 changes: 90 additions & 0 deletions test/browser/html5-routes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,96 @@ createTest('setRoute with a single parameter should change location correctly',
}, 14)
});

createTest('setRoute: replace a specific segment of the current route and change location correctly', {
'/authors/12/book/23': {
on: function() {
if (!browser_history_support) {
shared.fired.push(location.hash.replace(/^#/, ''));
} else {
shared.fired.push(location.pathname);
}
}
},
'/authors/12/video/23': {
on: function() {
if (!browser_history_support) {
shared.fired.push(location.hash.replace(/^#/, ''));
} else {
shared.fired.push(location.pathname);
}
}
}
}, function(assert) {
var self = this;
this.navigate('/authors/12/book/23', function() {
this.router.setRoute(2, 'video');
setTimeout(function() {
assert.deepEqual(shared.fired, ['/authors/12/book/23', '/authors/12/video/23']);
self.finish();
}, 14);
});
});

createTest('setRoute: remove segments of the current route and change location correctly', {
'/authors/12/book/23': {
on: function() {
if (!browser_history_support) {
shared.fired.push(location.hash.replace(/^#/, ''));
} else {
shared.fired.push(location.pathname);
}
}
},
'/authors/12': {
on: function() {
if (!browser_history_support) {
shared.fired.push(location.hash.replace(/^#/, ''));
} else {
shared.fired.push(location.pathname);
}
}
}
}, function(assert) {
var self = this;
this.navigate('/authors/12/book/23', function() {
this.router.setRoute(2, 2);
setTimeout(function() {
assert.deepEqual(shared.fired, ['/authors/12/book/23', '/authors/12']);
self.finish();
}, 14);
});
});

createTest('setRoute: insert and replace a segment into the current route and change location correctly', {
'/authors/12/book/23': {
on: function() {
if (!browser_history_support) {
shared.fired.push(location.hash.replace(/^#/, ''));
} else {
shared.fired.push(location.pathname);
}
}
},
'/authors/12/awesome': {
on: function() {
if (!browser_history_support) {
shared.fired.push(location.hash.replace(/^#/, ''));
} else {
shared.fired.push(location.pathname);
}
}
}
}, function(assert) {
var self = this;
this.navigate('/authors/12/book/23', function() {
this.router.setRoute(2, 2, 'awesome');
setTimeout(function() {
assert.deepEqual(shared.fired, ['/authors/12/book/23', '/authors/12/awesome']);
self.finish();
}, 14);
});
});

createTest('route should accept _ and . within parameters', {
'/:a': {
on: function root() {
Expand Down
69 changes: 69 additions & 0 deletions test/browser/routes-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,75 @@ createTest('setRoute with a single parameter should change location correctly',
}, 14)
});

createTest('setRoute: replace a specific segment of the current route and change location correctly', {
'/authors/12/book/23': {
on: function() {
shared.fired.push(window.location.hash);
}
},
'/authors/12/video/23': {
on: function() {
shared.fired.push(window.location.hash);
}
}
}, function(assert) {
var self = this;
shared.fired = [];
this.navigate('/authors/12/book/23', function() {
this.router.setRoute(2, 'video');
setTimeout(function() {
assert.deepEqual(shared.fired, ['#/authors/12/book/23', '#/authors/12/video/23']);
self.finish();
}, 14);
});
});

createTest('setRoute: remove segments of the current route and change location correctly', {
'/authors/12/book/23': {
on: function() {
shared.fired.push(window.location.hash);
}
},
'/authors/12': {
on: function() {
shared.fired.push(window.location.hash);
}
}
}, function(assert) {
var self = this;
shared.fired = [];
this.navigate('/authors/12/book/23', function() {
this.router.setRoute(2, 2);
setTimeout(function() {
assert.deepEqual(shared.fired, ['#/authors/12/book/23', '#/authors/12']);
self.finish();
}, 14);
});
});

createTest('setRoute: insert and replace a segment into the current route and change location correctly', {
'/authors/12/book/23': {
on: function() {
shared.fired.push(window.location.hash);
}
},
'/authors/12/awesome': {
on: function() {
shared.fired.push(window.location.hash);
}
}
}, function(assert) {
var self = this;
shared.fired = [];
this.navigate('/authors/12/book/23', function() {
this.router.setRoute(2, 2, 'awesome');
setTimeout(function() {
assert.deepEqual(shared.fired, ['#/authors/12/book/23', '#/authors/12/awesome']);
self.finish();
}, 14);
});
});

createTest('route should accept _ and . within parameters', {
'/:a': {
on: function root() {
Expand Down

0 comments on commit 278f6b7

Please sign in to comment.