Created
October 17, 2014 04:19
-
-
Save davidbiehl/488e0bcf97a2140c2b16 to your computer and use it in GitHub Desktop.
EventRouter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App.module "Lib", (Lib, App, Backbone, Marionette, $, _)-> | |
class Lib.EventRouter | |
constructor: (opts = {})-> | |
@events = _.extend(_.result(@, "events"), _.result(opts, "events")) | |
@vent = _.result(opts, "vent") || _.result(@, "vent") | |
@namespace = _.result(opts, "namespace") || _.result(@, "namespace") | |
@controller = _.result(opts, "controller") || _.result(@, "controller") || @router.controller | |
@event(name, route) for name, route of @events | |
events: {} | |
vent: false | |
namespace: false | |
router: false | |
controller: false | |
event: (name, route)-> | |
@vent.on "nav:#{@namespace}:#{name}", (args...)=> | |
@controller[name].apply(@controller, args) | |
nav = if _.isFunction(route) | |
route.apply(@, args) | |
else | |
route | |
Backbone.history.navigate nav | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment