Skip to content

Commit

Permalink
Merge a996994 into 573b967
Browse files Browse the repository at this point in the history
  • Loading branch information
giuliandrimba committed Jan 14, 2015
2 parents 573b967 + a996994 commit e07a391
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/ways.js
@@ -1,10 +1,14 @@
var way = require('./way'),
flow = require('./flow');
flow = require('./flow'),
happens = require('happens');

var mode, flo, middleware,
routes = [];

var e = happens({});

dispatch = function(url) {
e.emit("url:change", url);
var i, route;
url = '/' + url.replace(/^[\/]+|[\/]+$/mg, '');
for(i in routes) {
Expand All @@ -27,14 +31,17 @@ run = function(url, route) {
module.exports = function(pattern, runner, destroyer, dependency){
if(flo && arguments.length < 3)
throw new Error('In `flow` mode you must to pass at least 3 args.');

var route = way(pattern, runner, destroyer, dependency);
routes.push(route);
return route;
};

exports = module.exports;

exports.on = e.on.bind(e);
exports.off = e.off.bind(e);

exports.init = function() {
dispatch(this.pathname());
};
Expand All @@ -57,11 +64,16 @@ exports.pathname = function(){
return middleware.pathname();
};

exports.go = function(url, title, state){
exports.go = function(url, title, state) {

if(middleware)
{
middleware.push(url, title, state);
}
else
{
dispatch(url);
}
};

exports.go.silent = function(url, title, state){
Expand All @@ -74,4 +86,4 @@ exports.reset = function(){
mode = null
routes = []
middleware = null
};
};
37 changes: 37 additions & 0 deletions test/events.js
@@ -0,0 +1,37 @@
var ways = require('../lib/ways'),
should = require('chai').should();

describe('[events]', function(){

var out = null,
run = null,
destroy = null;

before(function(){

run = function(req) {

}

ways.reset()
ways('/', run);
ways('/pages', run);
});

it('should dispatch an event when the url changes', function(done) {

ways.on("url:change", function (pathname) {
pathname.should.equal('/pages')
done()
});

ways.go('/pages');
});

it('should remove event when the url changes', function(done) {

ways.off("url:change");
done()
ways.go('/pages');
});
});

0 comments on commit e07a391

Please sign in to comment.