Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable calling routie route declaration multiple times #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 27 additions & 12 deletions lib/routie.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
var map = {};
var reference = "routie";
var oldReference = w[reference];
var started = false;

var Route = function(path, name) {
this.name = name;
Expand All @@ -12,7 +13,6 @@
this.fns = [];
this.params = {};
this.regex = pathToRegexp(this.path, this.keys, false, false);

};

Route.prototype.addHandler = function(fn) {
Expand Down Expand Up @@ -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;
}
};

Expand Down Expand Up @@ -143,7 +158,7 @@
window.location.hash = path;

if (silent) {
setTimeout(function() {
setTimeout(function() {
addListener();
}, 1);
}
Expand Down Expand Up @@ -197,5 +212,5 @@
addListener();

w[reference] = routie;

})(window);
14 changes: 14 additions & 0 deletions test/routie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down