Since normally routing events are processed by callback functions, I
wonder what does it take to trigger these routing events from a
non-Giraffe Backbone application?
Anything with a reference to a router can cause
routing events, e.g.:
var app = new Giraffe.App({routes: {'post/:id': 'route:post'}});
// Trigger a route with an app reference
app.router.cause('route:post', 42); // => location changes to #post/42
// => 'route:post' triggered on `app`
Giraffe.Router#cause
is like Backbone.Events#trigger
with the addition of navigating to the corresponding route, if one exists, and the router
triggers the event on the app
, not itself.
Anything with the app reference can listen for a routing event:
// Handle the route from outside the Giraffe app
app.on('route:post', function(id) {...});
// Other `Backbone.Events` instances can listen to the Giraffe app
var myOtherApp = new Backbone.View;
myOtherApp.listenTo(app, 'route:post', function(id) {...});
The app also acts as a convenient event hub beyond routing events. All Giraffe objects have a reference to this.app
(if one has been created) and support for the shortcut appEvents
bindings.
Any issues that you see with this kind decoupling the router from application modules?
(I'm one of the authors) We at our company and I in my personal experience have not found this to be a problem, but one can imagine situations where this event-based system fails to provide the level of coordination needed. We've considered improving route handling with functionality like filters but haven't found the time for it yet. If you have suggestions we'd love to hear them!