fantactuka / backbone-route-filter Goto Github PK
View Code? Open in Web Editor NEWBefore and after sync/async filters for backbone router
License: MIT License
Before and after sync/async filters for backbone router
License: MIT License
var ApplicationController = Backbone.Router.extend({
before: {
'*any': '_logRoute'
},
_logRoute: function() {
...
}
});
var PostsController = ApplicationController.extend({
before: {
'*any-post': '_checkAuth'
},
_checkAuth: function() {
...
}
});
PostsController
before filter should also have '*any': '_logRoute'
inherited
Add note about how to run test.
I think karma config needs to be updated now.
JASMINE
and JASMINE_ADAPTER
are not supported anymore.
In backbone routes, the router stops matching in case it found a match. In case of the filter, it matched all the filters possible. For eg. If i have to match 'login' and then '*any', it will execute both the filter functions. Is there a way to stop the execution at the first match and not proceed.
https://github.com/chirag04/backbone-async-route-filter
Made a new module as adding async changed the philosophy of backbone-route-filter.
Async route filter uses express style next callback.
I have a RequireJS project and I encountered a situation where Backbone is loaded asynchronously via RequireJS after backbone-route-filter expects it to be available. If there is a way to load an AMD dependency before its non-AMD dependant, I haven't found it.
On that note, I've thrown together a quick way to make backbone-route-filter AMD compatible here. If you like, I can test this and open a pull request in a few days.
Thanks for the library!
Backbone 1.1 has a nice hook to bind additional logic to the router
Taking from the example below, lets say the checkAuthorization
is making a ajax request to check the authorization.
Since ajax is asynchronous, I cannot return from checkAuthorization until the ajax is done.
How do i handle such cases?
var Router = Backbone.Router.extend({
routes: {
'users': 'usersList',
'users/:id': 'userShow',
'account/sign-in': 'signIn'
},
before: {
// Using instance methods
'users(:/id)': 'checkAuthorization',
// Using inline filter definition
'*any': function(fragment, args) {
console.log('Atempting to load ' + fragment + ' with arguments: ', args);
}
},
after: {
// Google analytics tracking
// After filter will be triggered only if all before filters passed and action was triggered,
// so you'll only track pages that was displayed to user
'*any': function(fragment) {
goog._trackPageview(fragment);
}
},
checkAuthorization: function(fragment, args) {
if (!this._isSignedIn) {
// Going to sign-in page
this.signIn();
// Preventing current route from being handled
return false;
}
}
});
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.