From 5ed45cdaf9a8e0b4b62c9a4aeb78e912dc4a6b02 Mon Sep 17 00:00:00 2001 From: Rubens Pinheiro Date: Thu, 2 Oct 2014 22:17:16 -0300 Subject: [PATCH 1/2] Added a way to define the routes with multiple calls to routie. Added a third param to routie (forceCheck) default true, where if false, don't call the reload. Added method apply, to force reload but ensures a unique call to it. --- lib/routie.js | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/lib/routie.js b/lib/routie.js index 55071ba..45986e5 100644 --- a/lib/routie.js +++ b/lib/routie.js @@ -4,6 +4,7 @@ var map = {}; var reference = "routie"; var oldReference = w[reference]; + var started = false; var Route = function(path, name) { this.name = name; @@ -12,7 +13,6 @@ this.fns = []; this.params = {}; this.regex = pathToRegexp(this.path, this.keys, false, false); - }; Route.prototype.addHandler = function(fn) { @@ -97,17 +97,32 @@ map[path].addHandler(fn); }; - var routie = function(path, fn) { - if (typeof fn == 'function') { - addHandler(path, fn); - routie.reload(); - } else if (typeof path == 'object') { - for (var p in path) { - addHandler(p, path[p]); + var routie = function(path, fn, forceCheck) { + if(typeof forceCheck === 'undefined'){ + forceCheck = true; + } + if (typeof fn == 'function') { + addHandler(path, fn); + if(forceCheck){ + routie.reload(); + } + } else if (typeof path == 'object') { + for (var p in path) { + addHandler(p, path[p]); + } + if(forceCheck){ + routie.reload(); + } + + } else if (typeof fn === 'undefined') { + routie.navigate(path); } + }; + + routie.apply = function () { + if(!started){ routie.reload(); - } else if (typeof fn === 'undefined') { - routie.navigate(path); + started = true; } }; @@ -143,7 +158,7 @@ window.location.hash = path; if (silent) { - setTimeout(function() { + setTimeout(function() { addListener(); }, 1); } @@ -197,5 +212,5 @@ addListener(); w[reference] = routie; - + })(window); From 4d0af48567c988a4128ee1cb27650719d75527ee Mon Sep 17 00:00:00 2001 From: Rubens Pinheiro Date: Thu, 2 Oct 2014 22:26:08 -0300 Subject: [PATCH 2/2] Test multiple defines to routie --- test/routie.test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/routie.test.js b/test/routie.test.js index 9007d60..0a144c9 100644 --- a/test/routie.test.js +++ b/test/routie.test.js @@ -48,6 +48,20 @@ suite('routie', function() { window.location.hash = 'test8'; }); + test('calling routes multiple times, executing call only once', function(done){ + var runCount = 0; + routie('test21', function(){ + runCount++; + }, false); + + routie('test22', function(){}, false); + + routie.apply(); + window.location.hash = 'test21'; + assert.equal(runCount, 1); + }); + + test('trigger hash', function(done) { routie('test3'); setTimeout(function() {