Last Updated: February 25, 2016
·
2.125K
· vrinek

`rake routes` for Backbone

In case you are developing a Backbone on Rails type of application, one of the things that you will have duplicates of are your routes. Rails provides you with a rake routes command that will display all known routes.

The equivalent of rake routes for Backbone can be achieved with:

_.map(Backbone.history.handlers, function(i){return i.route})

With this, you have a list of regular expressions to try and match to your rails routes.

Example script to make the comparison (needs to be adapted to your needs):

backbone_routes = [
  /^projects\/([^\/]+)$/,
  /^projects\/([^\/]+)\/details$/,
  /^admin$/
]

rails_routes = Rails.application.routes.routes.to_a.map do |route|
  route.path.spec.to_s.sub(/\([^\)]+\)/, '')
end

backbone_routes.each do |regexp|
  matching_route = rails_routes.find { |route| route =~ regexp }
  puts matching_route || "NO MATCH FOUND - #{regexp.inspect}"
end

One thing to watch for when copying the Backbone routes (from the browser's JavaScript console) is that [^/] has to be substituted with [^\/] in Ruby to work.

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside