Skip to content
Browse files

Introducing Main and Home routers

  • Loading branch information...
1 parent 78fab71 commit d24d00aaaead5ba899e6774914eb76164a32e6fd @ellmo committed Apr 25, 2012
View
6 app/assets/javascripts/backbone/initalizer.coffee
@@ -1,3 +1,7 @@
class @Rbbe.Initializer
constructor: ->
- alert "Oh, my god, yes!\nOh, my god!"
+ @router = new Rbbe.Routers.Main()
+ @controller = $('body').data('controller')
+ @action = $('body').data('action')
+ @router[@controller](@action)
+
View
4 app/assets/javascripts/backbone/routers/home_router.coffee
@@ -0,0 +1,4 @@
+class @Rbbe.Routers.Home extends Backbone.Router
+
+ index: ()->
+ alert "I'm in the Home router, woo hoo!"
View
8 app/assets/javascripts/backbone/routers/main_router.coffee
@@ -0,0 +1,8 @@
+class @Rbbe.Routers.Main extends Backbone.Router
+
+ routes:
+ 'home/:action': 'home'
+
+ home: (action) ->
+ @home_router ?= new Rbbe.Routers.Home()
+ @home_router[action]() if @home_router[action]?
View
2 app/views/layouts/application.haml
@@ -5,5 +5,5 @@
= stylesheet_link_tag "application"
= javascript_include_tag "application"
= csrf_meta_tags
- %body
+ %body{:data => { :controller => controller_name, :action => action_name }}
= yield

5 comments on commit d24d00a

@ellmo
Owner
ellmo commented on d24d00a Apr 25, 2012

Now we're starting to get somewhere!

The first place you need to look is the app/views/layouts/application.haml change, which adds two simple attributes to the HTML's body tag. This is the simplest way to pass these parameters to JavaScript and to make sure they can be accessed in every view.

@ellmo
Owner
ellmo commented on d24d00a Apr 26, 2012

The next big thing is the Initializer class, which grabs values in those fields by doing @controller = $('body').data('controller') and @action = $('body').data('action') and passes them to... what? A Rbbe.Routers.Main class object?

What is this sorcery?

@ellmo
Owner
ellmo commented on d24d00a Apr 26, 2012

Routers in Backbone.js are somewhat similar to Controllers in Rails. Somewhat, because they also do most of that stuff that Rails' config/routes.rb does. That's of course because Backbone.js doesn't have a server. Rails does and it uses that server to control the request traffic based on the routes.rb.

My personal opinion is that having a Main router makes it a bit similar to having this centralised structure that makes decisions where the traffic is supposed to go, before it hits any real portion of logic-related code. It's like having a little piece of Rack server, spawned in your browser.

@ellmo
Owner
ellmo commented on d24d00a Apr 26, 2012

So this Initializer class calls for a new instance of Routers.Main class and passes the @controller and @action to it.

Now, it's true that @router doesn't have to be a Backbone-based class. But because we use the Rbbe.Routers namespace, we can easily access it through the browser's JS console, should we have any need to debug it.

When the @controller and @action parameters are passed, Main router calls for the proper Router, linked with the currently used Rails Controller.

@ellmo
Owner
ellmo commented on d24d00a Apr 26, 2012

When all this is done and we fire up the application and open localhost:3000, Rails router will direct you to the home#index view. And when this happens, Backbone's Main Router will promptly call for a new instance of Rbbe.Routers.Home class and in this Router will call for index() method.

Please sign in to comment.
Something went wrong with that request. Please try again.