Skip to content
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
22 changes: 22 additions & 0 deletions lib/davis.app.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ Davis.App = (function () {
}

var handleRequest = function (request) {
if (!self.running) return;

if (beforeFiltersPass(request)) {
self.trigger('lookupRoute', request)
var route = self.lookupRoute(request.method, request.path);
Expand Down Expand Up @@ -216,5 +218,25 @@ Davis.App = (function () {
this.running = false
};

/**
* Destroys the app.
*
* Stops the app and cleans itself from any DOM interaction bringing it back to an unconfigured state.
*/
App.prototype.destroy = function() {
this.stop();

if (this.boundToInternalEvents) {
this
.unbind('runRoute')
.unbind('routeNotFound')
.unbind('start')
.unbind('stop')
.unbind('routeError');

this.boundToInternalEvents = false;
}
};

return App;
})()
14 changes: 14 additions & 0 deletions lib/davis.event.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ Davis.event = function () {
handlers[i].apply(this, args)
}

return this;
};

/**
* Unbinds all the callbacks from an event.
*
* @param {String} event event name
* @memberOf event
*/
this.unbind = function (event) {
if (callbacks[event]) {
delete callbacks[event];
}

return this;
};
}
15 changes: 15 additions & 0 deletions tests/test_app.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,19 @@ test("adding helpers to requests", function () {

var req = factory('request')
equal('bar', req.foo)
})

test("should cleanup internal events when stopped", function () {
var app = factory('app'),
unbindCount = 0;

app.unbind = function () {
unbindCount += 1;
return app;
}

app.start();
app.destroy();
ok(!app.boundToInternalEvents, "should set the internal events flag to false");
equals(unbindCount, 5, "should unbind every internal events");
})
16 changes: 15 additions & 1 deletion tests/test_event.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,18 @@ test("passing data to an event handler", function () {
same({foo: 'bar'}, callbackData, "should pass the callback data through to the handler");
same({blah: 'halb'}, moreCallbackData, "should pass the callback data through to the handler");
same(events, callbackContext, "should have this set to the app object");
})
})

test("Unbindind an event", function () {
var events = factory('events'),
callbackCalled = false;

events.bind('foo', function () {
callbackCalled = true;
});

events.unbind('foo');
events.trigger('foo');

ok(!callbackCalled, "callback should have been unbound and not called when triggered");
});