Quantcast
Channel: JavaScriptMVC Forum
Viewing all articles
Browse latest Browse all 3491

Re : can.route handling specific routes

$
0
0
Hi

I have also struggled with the canjs routing system, ref my post https://forum.javascriptmvc.com/#Topic/32525000001153061 

Try this implementation. 

The problem with
 so if you are on the #!bar/blah route want to invoke a different route later with can.route.attr({type:"foo", view:"something"}, true)
 

 should work now and you will also see that the correct listeners are called when you change the hash, like window.location.hash = '#!foo/something' or manually in the address bar.

I have also created a listener that matches empty hash, #, or #!

Note, I do not create a default route, from what I understand it will create a default route when you have a listener like this ':type/:view'.You could also set default values for the route like this: can.route('', {type:'auth', view:'logon'}); . Notice that this is set in the init method.

  1. var App = can.Control({
  2.         'init': function() {
  3.             can.route('', {type:'auth', view:'logon'});
  4.         },
  5.         'auth/:view route': function(data) {
  6.             // default actions on index page (logon page)
  7.             switch(data.view) {
  8.                 case 'logon':
  9.                     $('#pagecontent').html("auth/" + data.view);
  10.                     break;
  11.                 default:
  12.                     $('#pagecontent').html("Unknown route: auth/" + data.view);
  13.                     break;
  14.             }
  15.         },
  16.         'foo/:view route': function (data) {
  17.             $('#pagecontent').html("foo/" + data.view);
  18.         },
  19.         'bar/:view route': function (data) {
  20.             switch(data.view) {
  21.                 case 'first':
  22.                     $('#pagecontent').html("bar/" + data.view);
  23.                     break;
  24.                 case 'second':
  25.                     $('#pagecontent').html("bar/" + data.view);
  26.                     break;
  27.                 default:
  28.                     $('#pagecontent').html("Unknown route: bar/" + data.view);
  29.                     break;
  30.             }
  31.         },
  32.         'route': function(data) {
  33.             $('#pagecontent').append("Empty route fired");
  34.         },
  35.         ':type/:view route': function(data) {
  36.             $('#pagecontent').append("Generic route fired -> not matching any of the other listeners");
  37.         }
  38.     });

  39.     can.route.ready(false);
  40.     new App( document.body );    // create the application + routing controller
  41.     can.route.ready(true);
Does this work for you? At least got you one step closer to what you want to achieve :)?


Viewing all articles
Browse latest Browse all 3491

Trending Articles