Backbone & Rails. Why do you set routes in backbone? - ruby-on-rails

js and I want to create a rails app with backbone. The only problem is, I cant seem to figure out what goes in the backend and what goes in the frontend. The approach I am using is to use rails essentially as an API.
I am looking at various different approaches and I cant seem to quite understand why you route in backbone as well as using normal rails routing.
My theory is:
You use rails to display initial html pages, and you use backbone routing to route the javascript files to that html pages so you can perform DOM manipulation there.
Here is the part of my code where I got that idea from. (NOTE: this is all mainly taken from Ryan Bates railcast on backbone.js)
backbone router
class Poster.Routers.Posts extends Backbone.Router
routes:
'':'index'
'posts/:id': 'show'
initialize: ->
#collection = new Poster.Collections.Posts()
#collection.fetch({reset: true})
index: ->
view = new Poster.Views.PostsIndex(collection: #collection)
$('#index_container').html(view.render().el)
views/main/index.html.erb
<div id="index_container"></div>
So does this backbone routing essential do the equivalent of just loading the javascript in the index: method at the top of the views/main/index.html.erb? (i know actually copying those javascript lines into the index.html.erb file wont work, its just a conceptual question)

Backbone is designed to be used in single-page applications, therefore it has a little to do with Rails routing. All Rails has to do is land a .html page, and everything else is client's concern.
If you intend to use Rails as an API, then its routing does not matter at all. You can even keep Rails application and frontend on different servers. In this case all what Rails has to do is process requests from the client.
For example, you can build your client-side without any hard ties to a backend, they can be absolutely separated. You just make AJAX calls from a client, then server processes them and responds with JSON.
Also, Backbone does not "route the javascript files to that html pages". It just executes functions according to a hash "route: action". These are just plain JavaScript functions which already can "see" JavaScript working within the document.

Related

Best practice for multi-page application using Rails and Angular?

There are tons of tutorials out there for creating single page applications using Rails and Angular. They usually pass all the routing to Angular and leave Rails for the activerecord stuff. I find this frustrating because there are times when I just want a plain HTML page with minimal client side bells and whistles.
What is the best practice for a multi-page application, where I can use both Rails routes and Angular UI-router?
I would use the Angular router so all of your routes are in one place. The best part about angular is you can add as much or as little as you want. So you can have static pages handled by the angular router.

Using AngularJS in Ruby on Rails app

I have a existing project in Ruby on Rails.
What is the best way to use AngularJS in Ruby on Rails app?
I want to use AngularJs for only specified modules, not for create SPA. This is good way?
What I have seen colleagues do in order to achieve this sort of integration with an existing rails app is:
Include angular.js and relevant dependencies in the specific app pages that are to be 'angularized'
Interpolate whatever data is needed to bootstrap the angular controller into the html template which contains the angular app. This might include data about the resource being operated on. I've seen this done by rendering a RABL template inside of a haml/erb template.
Using that interpolated data, call whatever API methods you need to get additional data on the fly. This is usually just a matter of implementing json handlers for routes you've already created.
I can't say whether this is best practice, but its an easy way to get started fast.
Best of luck, angular is a very enjoyable tool to work with once you get used to it.

Am I handling the architecture of my Rails site correct?

this is a rather subjective question. I am looking for expert opinion because this is the first time I'm architecting an enterprise rails application.
I decided to make my site completely AJAX based and I'm not using Rails AJAX helpers at all. Reason being: I'm new to web development and I like to know what's happening behind the code I'm writing.
Also, this is a fairly large dashboard based analytics application and I'm absolutely not using any erb tags. All data comes and goes using AJAX.
So, whenever I need any data, I write a rails route like this
match "people/all" => "people#all"
respond_to do |format|
format.json { all json is rendered here }
end
Same goes for POST calls.
Am I doing it right?
The next thing I'm going to do is to add CSRF token security ( if it doesn't match, controller will send no AJAX back )
I'm also driving an API for mobile apps off this project but the API is protected with keys.
I know maybe something like ember or a micro framework could have been a better choice but I chose rails because my application is going to have loads of features so I stuck with rails instead of diving into another framework.
So,
Is it in some way bad to make rails views without erb tags and do everything with AJAX?
Is my app going to be vulnerable in any way?
thanks.
You shouldn't be doing any of this yourself, Rails does this for you, including handling CSRF tokens. Your application controller should already be doing this.
Use resources :people in your routes. The route to "get all people" should simply be /people, not /people/all.
Examine a scaffold-generated controller to figure out how your routes should map to the seven default RESTful actions.

Ruby on Rails + Backbone.js Routing

I'm trying to integrate backbone.js as the frontend framework for my rails app. I have a lists model (among others) in rails.
When setting up my backbone frontend, I'm using history pushState as opposed to hashtags. I want backbone/js to handle a lot of the front end (users logged in affects the header, etc.) and have the app have the 'app like feel'.
However, when accessing /lists it forwards to the rails controller, and not backbone. Is there a solution around this so I can use the built in sessions with rails and backbone, when the urls for rails and the front end have the same name?
To anybody looking for an answer: the solution we used was just let Backbone control everything except login/register forms using API calls our iOS app also uses

Best practice for rendering data in the Backbone view templates

I was using backbone standalone for some time but currently I am trying to integrate it with Rails. Until now I used underscore templates and the question would be if it is possible to use Rails view helpers inside the template and if it is smart thing to do at all?
Update: Here is a simple example what I am talking about.
I have a list of messages and I have a MessageView for each message, I want to render the avatar thumbnail of the message author, link to his profile and description when the message was posted. Also I use markdown for the message content. With underscore templates I don't have access to the helpers to achieve this so I am forced to create methods on the model itself which feels really wrong...
You should take a look at the EJS Embedded JavaScript Framework, which provides rails-like standard view helpers like link_to, url_for, and other form tags.
Of course, you will have to translate your custom rails templates in js, but it's a start !
I ran into the same problem where I wanted to reuse my templates between Backbone and Rails. I ran into stache before: https://github.com/agoragames/stache
You can read more about the setup here: http://slainer68.wordpress.com/2011/09/20/partial-reuse-between-rails-js-the-easy-way/
Right out of the box, your underscore templates are pure javascript, so, in that sense, you can't really embed rails helpers into them. You can, however, make those templates ejb's (or whatever templating system you use) and have rails render them. With so little information, it's impossible to figure out what your app does, but it does feel weird to me to do that. I think, typically, your javascript templates are used for rendering html on the host side after some js functionality. Maybe a better description of what you are trying to accomplish?
Update ...
So you have some set of relationships between messages and authors in your rails models correct? You'd do a similar thing in your backbone models. So, you've got a User model, and a Message model. User has_many Messages, and Message has_one User. You can model that out in backbone as well... see my answer here:
Backbone set collection attribute (for the url)
You just need to describe the relationship on the backbone side.

Resources