« Back
Sammy.Application around ( callback )
Adds an around filter to the application. around filters are functions
that take a single argument callback which is the entire route
execution path wrapped up in a closure. This means you can decide whether
or not to proceed with execution by not invoking callback or,
more usefuly wrapping callback inside the result of an asynchronous execution.
Example
The most common use case for around() is calling a possibly async function and executing the route within the functions callback:
var app = $.sammy(function() {
var current_user = false;
function checkLoggedIn(callback) {
// /session returns a JSON representation of the logged in user
// or an empty object
if (!current_user) {
$.getJSON('/session', function(json) {
if (json.login) {
// show the user as logged in
current_user = json;
// execute the route path
callback();
} else {
// show the user as not logged in
current_user = false;
// the context of aroundFilters is an EventContext
this.redirect('#/login');
}
});
} else {
// execute the route path
callback();
}
};
this.around(checkLoggedIn);
});
comments powered by Disqus