Want more features on Pastebin? Sign Up, it's FREE!
Guest

Meteor Router with history support

By: a guest on Jan 5th, 2013  |  syntax: CoffeeScript  |  size: 0.79 KB  |  views: 47  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ###
  2.   MainApp Router
  3. ###
  4. AppRouter = Backbone.Router.extend
  5.   routes:
  6.     '*path' : 'main'
  7.  
  8.   main: (url_path) ->
  9.     Session.set 'page_id', url_path
  10.  
  11. Router = new AppRouter
  12.  
  13.  
  14. ###
  15.   History Support
  16. ###
  17. Backbone.history.start
  18.   pushState: true
  19.  
  20. Session.set 'page_url', window.location.pathname
  21.  
  22. ###
  23.   Navigation - Listen for changes to Session.page_url and echo to Router
  24. ###
  25. Meteor.autorun (handle) ->
  26.   page_url        = Session.get 'page_url'
  27.   document.title  = page_url
  28.  
  29.   Router.navigate page_url, true
  30.  
  31. ###
  32.   Pathchange Support
  33. ###
  34. $ () ->
  35.   $.pathchange.init() # setup event listeners, etc.
  36.  
  37.   $(window).pathchange(() ->
  38.     if Session.get('page_url') isnt window.location.pathname
  39.       Session.set 'page_url', window.location.pathname
  40.   ).trigger "pathchange"
clone this paste RAW Paste Data