Backbone.js and Rails 3 how to structure app with authentication?
I am doing a simple Rails app with devise where users are able to sign up and sign in. Moreover, I have a gue开发者_开发问答st user (so users can try the app without being registered).
After signing in, a user is redirected to the dashboard. Here I use backbone.js to handle interactions. I have the following pages and routes:
Rails:
home: http://myapp.com/
sign in: http://myapp.com/users/sign_in
sign up: http://myapp.com/users/sign_up
Backbone:
dashboard: http://myapp.com/
new model: http://myapp.com/#new
model detail: http://myapp.com/#model/:id
In the Backbone Router I specify a function to execute when users go to root path (both the home page and the dashboard), but it is only needed in the dashboard.
I am using both rails and backbone to specify routes. Is this correct? Should I have different routes for home page and dashboard? Use backbone in the entire app? What is the best way to organize an app like this?
Any article or book about this?
Thanks!
I have setup a few Backbone + Rails apps. Yes, you need both Rails and Backbone handling 'routes' - however, Backbone responds to hash changes in URLs which is a bit different from Rails routing. For your setup, I would do the following:
In the controller that serves 'home', if a user is logged in, render the homepage, otherwise render a page that has the js to load your Backbone application. Have the default route('')in Backbone load the dashboard view. Keep the sign in and out pages the way they are setup with Devise.
This should answer some of your questions: http://codetunes.com/2012/01/03/backbone-js-rails-3-asynchronous-interfaces
精彩评论