Guest User

Untitled

a guest
Sep 26th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. MyApplication = Luca.Application.extend
  2. initialize: (@options={})->
  3. @router = new Luca.Router(app: @)
  4.  
  5. @router.bind "change:navigation", ()=>
  6. @updateYourGoogleAnalytics()
  7.  
  8. Luca.Router = Backbone.Router.extend
  9.  
  10. routes:
  11. "" : "home"
  12. "whatever" : "whatever"
  13.  
  14. initialize: (@options)->
  15. _.extend @, @options
  16.  
  17. @routeHandlers = _( @routes ).values()
  18.  
  19. # when a route handler is fired, the route:route_name event is triggered by the router
  20. # unfortunately this doesn't apply to calls to @navigate() so we override Backbone.Router.navigate
  21. # and trigger an event separately.
  22. _( @routeHandlers ).each (route_id) =>
  23. @bind "route:#{ route_id }", ()=>
  24. @trigger.apply @, ["change:navigation", route_id ].concat( _( arguments ).flatten() )
  25.  
  26.  
  27. # Intercept calls to Backbone.Router.navigate so that we can at least
  28. # build a path from the route, even if we don't trigger the route handler
  29. navigate: (route, triggerRoute=false)->
  30. Backbone.Router.prototype.navigate.apply @, arguments
  31. @buildPathFrom( Backbone.history.getFragment() )
  32.  
  33. # given a url fragment, construct an argument chain similar to what would be
  34. # emitted from a normal route:#{ name } event that gets triggered
  35. # when a route is actually fired. This is used to trap route changes that happen
  36. # through calls to @navigate()
  37. buildPathFrom: (matchedRoute)->
  38. _(@routes).each (route_id, route)=>
  39. regex = @_routeToRegExp(route)
  40. if regex.test(matchedRoute)
  41. args = @_extractParameters(regex, matchedRoute)
  42. @trigger.apply @, ["change:navigation", route_id].concat( args )
Add Comment
Please, Sign In to add comment