Commit 4b8c8371 authored by Thomas Lechauve's avatar Thomas Lechauve

Fix infinite loop while route is unknown

parent 7f42bc1b
......@@ -23,7 +23,20 @@ $.extend({
},
},
i = this.list[level].length;
if (this.exist(r) === false) {
this.list[level][i] = r;
}
},
exist: function (r) {
var found = false, i = 0;
while (i < this.list[r.level].length && found === false) {
if (this.list[r.level][i].route === r.route) {
found = true;
}
i += 1;
}
return found;
},
clean: function (level) {
......@@ -34,14 +47,16 @@ $.extend({
this.list = this.list.slice(0, 0);
},
search: function (hash) {
search: function (hash, level) {
var stop = false,
i, j,
regex,
result,
extracted;
level = level || 0;
i = this.list.length - 1;
while ((stop === false) && (i >= 0)) {
hash.route = hash.route === "undefined" ? "/" : hash.route;
while ((stop === false) && (i >= level)) {
j = 0;
while ((stop === false) && (j < this.list[i].length)) {
extracted = $.router.extractKeys(this.list[i][j].route);
......@@ -54,12 +69,25 @@ $.extend({
hash[extracted.keys[k]] = result[k];
}
this.current = this.list[i][j];
this.clean(this.list[i][j].level + 1);
console.log(this.list[i][j].route)
this.list[i][j].callback(hash);
}
j += 1;
}
i -= 1;
}
},
isLastLevel: function () {
return this.current.level === (this.list.length - 1);
}
},
start: function (hash, level) {
var hashInfo = this.parseHash(hash);
if ($.router.routes.current.route !== hashInfo.route) {
this.routes.search(hashInfo, level);
}
},
......
......@@ -36,6 +36,7 @@ $(function () {
equal($.router.routes.list.length, 0, 'should remove all routes');
});
module('search tests', {
teardown: function () {
$.router.routes.cleanAll();
......@@ -43,24 +44,23 @@ $(function () {
});
test('search test', function () {
var url1 = {'route': '#/new/path', 'param1': 'foo1', 'param2': 'foo2', 'filter': true},
url2 = {'route': '#/new/path/1', 'param1': 'foo1'},
url3 = {'route': '#/new/path/1/foo', 'param1': 'foo1'},
var url1 = {'route': '/new/path', 'param1': 'foo1', 'param2': 'foo2', 'filter': true},
url2 = {'route': '/new/path/1', 'param1': 'foo1'},
url3 = {'route': '/new/path/1/foo', 'param1': 'foo1'},
spy = sinon.spy();
$.router.routes.add('#/new/path', 0, spy);
$.router.routes.add('#/new/path/:id', 1, spy);
$.router.routes.add('#/new/path/:id/:other', 2, spy);
$.router.routes.add('/new/path', 0, spy);
$.router.routes.search(url1);
delete url1.route;
ok(spy.calledWith(url1));
$.router.routes.add('/new/path/:id', 1, spy);
$.router.routes.search(url2);
delete url2.route;
$.extend(url2, {'id': '1'});
ok(spy.calledWith(url2));
$.router.routes.add('/new/path/:id/:other', 2, spy);
$.router.routes.search(url3);
delete url3.route;
$.extend(url3, {'id': '1', 'other': 'foo'});
......@@ -69,6 +69,19 @@ $(function () {
ok(spy.calledThrice);
});
test('avoid loop test', function () {
var spy = sinon.spy(),
i = 0,
callback = function (params) {
console.log(params)
$.router.routes.add('/first', 1, spy)
$.router.start(params.route);
}
$.router.routes.add('/', 0, callback);
$.router.routes.search({'route': '/first'});
ok(spy.calledOnce);
});
module('router methods tests', {
});
......@@ -103,4 +116,6 @@ $(function () {
deepEqual(response, urls[url]);
}
});
});
......@@ -282,6 +282,10 @@
</div>
</script>
<script id="topbar" type="text/html">
</script>
<script id="root" type="text/html">
<div id="loading" style="position: absolute; right: 20px; top: 20px;"></div>
<div class="navbar">
......
......@@ -94,6 +94,9 @@
nextLevel = route.level + 1;
$(this).vifib('render', 'root');
$.router.routes.add('/catalog', nextLevel, methods.showCatalog, $("#main"));
$.router.routes.add('/dashboard', nextLevel, methods.showDashboard, $("#main"));
// default page
$.router.start(params.route, nextLevel);
},
genInstanceUrl: function (uri) {
......@@ -149,15 +152,14 @@
$(this).vifib('render', 'catalog.all');
for (i=0; i<14; i++) {
item = $(this).vifib('getRender', 'catalog.item');
console.log(item)
$("#catalog-all").append(item);
}
});
},
showCatalog: function () {
showCatalog: function (params) {
return this.each(function () {
var i, item;
var i, item, nextLevel;
$(this).vifib('render', 'catalog.preview');
for (i=0; i<2; i++) {
item = $(this).vifib('getRender', 'catalog.item');
......@@ -171,6 +173,9 @@
item = $(this).vifib('getRender', 'catalog.categorie');
$("#catalog-categories").append(item);
}
nextLevel = $.router.routes.current.level + 1;
$.router.routes.add('/catalog/all', nextLevel, methods.showCatalogAll, $(this));
$.router.start(params.route, nextLevel);
});
},
......
......@@ -23,7 +23,20 @@ $.extend({
},
},
i = this.list[level].length;
if (this.exist(r) === false) {
this.list[level][i] = r;
}
},
exist: function (r) {
var found = false, i = 0;
while (i < this.list[r.level].length && found === false) {
if (this.list[r.level][i].route === r.route) {
found = true;
}
i += 1;
}
return found;
},
clean: function (level) {
......@@ -34,14 +47,16 @@ $.extend({
this.list = this.list.slice(0, 0);
},
search: function (hash) {
search: function (hash, level) {
var stop = false,
i, j,
regex,
result,
extracted;
level = level || 0;
i = this.list.length - 1;
while ((stop === false) && (i >= 0)) {
hash.route = hash.route === "undefined" ? "/" : hash.route;
while ((stop === false) && (i >= level)) {
j = 0;
while ((stop === false) && (j < this.list[i].length)) {
extracted = $.router.extractKeys(this.list[i][j].route);
......@@ -54,12 +69,25 @@ $.extend({
hash[extracted.keys[k]] = result[k];
}
this.current = this.list[i][j];
this.clean(this.list[i][j].level + 1);
console.log(this.list[i][j].route)
this.list[i][j].callback(hash);
}
j += 1;
}
i -= 1;
}
},
isLastLevel: function () {
return this.current.level === (this.list.length - 1);
}
},
start: function (hash, level) {
var hashInfo = this.parseHash(hash);
if ($.router.routes.current.route !== hashInfo.route) {
this.routes.search(hashInfo, level);
}
},
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment