Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/a/8241015/256772">Robert's answer</a> is interesting, but sadly it will only work if the hash is exactly as defined in the route. If you for example have a route for <code>user(/:uid)</code> it won't be matched if the <code>Backbone.history.fragment</code> is either <code>"user"</code> or <code>"user/1"</code> (both which are the two most obvious use cases for such route). In other words, it'll only find the appropriate callback name if the hash is exactly <code>"user(/:uid)"</code> (highly unlikely).</p> <p>Since i needed this functionality i extended the <code>Backbone.Router</code> with a <code>current</code>-function that reuses some of the code the History and Router object use to match the current fragment against the defined Routes for triggering the appropriate callback. For my use case, it takes the optional parameter <code>route</code>, which if set to anything truthful will return the corresponding function name defined for the route. Otherwise it'll return the current hash-fragment from <code>Backbone.History.fragment</code>.</p> <p>You can add the code to your existing Extend where you initialize and setup the Backbone router.</p> <pre><code>var Router = new Backbone.Router.extend({ // Pretty basic stuff routes : { "home" : "home", "user(:/uid)" : "user", "test" : "completelyDifferent" }, home : function() { // Home route }, user : function(uid) { // User route }, // Gets the current route callback function name // or current hash fragment current : function(route){ if(route &amp;&amp; Backbone.History.started) { var Router = this, // Get current fragment from Backbone.History fragment = Backbone.history.fragment, // Get current object of routes and convert to array-pairs routes = _.pairs(Router.routes); // Loop through array pairs and return // array on first truthful match. var matched = _.find(routes, function(handler) { var route = handler[0]; // Convert the route to RegExp using the // Backbone Router's internal convert // function (if it already isn't a RegExp) route = _.isRegExp(route) ? route : Router._routeToRegExp(route); // Test the regexp against the current fragment return route.test(fragment); }); // Returns callback name or false if // no matches are found return matched ? matched[1] : false; } else { // Just return current hash fragment in History return Backbone.history.fragment } } }); // Example uses: // Location: /home // console.log(Router.current()) // Outputs 'home' // Location: /user/1 // console.log(Router.current(true)) // Outputs 'user' // Location: /user/2 // console.log(Router.current()) // Outputs 'user/2' // Location: /test // console.log(Router.current(true)) // Outputs 'completelyDifferent' </code></pre> <p>I'm sure some improvements could be made, but this is a good way to get you started. Also, it's easy to create this functionality without extending the Route-object. I did this because it was the most convenient way for my set-up.</p> <p><strong>I haven't tested this fully yet, so please let me know if anything goes south.</strong></p> <hr> <h1>UPDATE 04/25/2013</h1> <p>I did some changes to the function, so instead of returning either the hash or route callback name, i return an object with fragment, params and route so you can access all the data from the current route, much like you would from the route-event.</p> <p>You can see the changes below:</p> <pre><code>current : function() { var Router = this, fragment = Backbone.history.fragment, routes = _.pairs(Router.routes), route = null, params = null, matched; matched = _.find(routes, function(handler) { route = _.isRegExp(handler[0]) ? handler[0] : Router._routeToRegExp(handler[0]); return route.test(fragment); }); if(matched) { // NEW: Extracts the params using the internal // function _extractParameters params = Router._extractParameters(route, fragment); route = matched[1]; } return { route : route, fragment : fragment, params : params }; } </code></pre> <p>See previous code for further comments and explanations, they look mostly the same.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload