router.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. var Backbone = require('backbone');
  2. var AppState = require('./app_state');
  3. var Pubsub = require('./lib/pubsub');
  4. var Router = Backbone.Router.extend({
  5. initialize: function() {
  6. var bp = function(p) {
  7. // remove leading slash
  8. return AppState.basePath(p).substring(1);
  9. };
  10. this.route(bp('/'), 'topics');
  11. this.route(bp('/topics/(:topic)(/:channel)'), 'topic');
  12. this.route(bp('/lookup'), 'lookup');
  13. this.route(bp('/nodes(/:node)'), 'nodes');
  14. this.route(bp('/counter'), 'counter');
  15. // this.listenTo(this, 'route', function(route, params) {
  16. // console.log('Route: %o; params: %o', route, params);
  17. // });
  18. },
  19. start: function() {
  20. Backbone.history.start({
  21. 'pushState': true
  22. });
  23. },
  24. topics: function() {
  25. Pubsub.trigger('topics:show');
  26. },
  27. topic: function(topic, channel) {
  28. if (channel !== null) {
  29. Pubsub.trigger('channel:show', topic, channel);
  30. return;
  31. }
  32. Pubsub.trigger('topic:show', topic);
  33. },
  34. lookup: function() {
  35. Pubsub.trigger('lookup:show');
  36. },
  37. nodes: function(node) {
  38. if (node !== null) {
  39. Pubsub.trigger('node:show', node);
  40. return;
  41. }
  42. Pubsub.trigger('nodes:show');
  43. },
  44. counter: function() {
  45. Pubsub.trigger('counter:show');
  46. }
  47. });
  48. module.exports = new Router();