I have several years of Rails development and few days Backbone.js experience. After these few days, I think I came into the flavor of Backbone.js. I was wondering about the kind of duplication which appears developing Backbone.js rich client scripts with Rails on the back. Lot of the models gets partly reimplemented on the Backbone side (model structure and validations).
Do you know tool, gem which would handle this duplication? Or generally does it make sense to tackle this?
Sorry for a potential speculative question. I am wondering whether someone tried to tackle question of Rails/Backbone model duplication.
You don't have to create a duplicate model architecture for Backbone, although that is usually the simplest way to go. (There are js extensions to backbone that add has_many and whatnot, if you are going that route: https://github.com/dbrady/ligament.js, and https://github.com/PaulUithol/Backbone-relational)
DISCLAIMER: Don't do the following, it sucks.
But you can pass whatever JSON to backbone that you like, and call that a "model", and make a separate controller to handle saving whatever you put in there. That can be much more messy, but more succinct if you want to send a glob of JSON to backbone and keep it in one model.
Example:
# controller
def index
@json = User.first.to_json(:include => {:address => {}, :contacts => {})
end
Send that JSON to backbone, and you have an object with nested relations, although it's virtually meaningless to Backbone without some way to maintain relations, so you'd have to extract the bits you want to deal with in JavaScript, put them back together, and make your create/update actions in Rails quite fancy to handle that...
But the point is that you can arbitrarily create whatever type of JSON object you want--because Backbone knows nothing about what Rails will do with it. Backbone just gets a URL to manage its HTTP requests, and trusts that the server is set up to handle whatever it throws at it. So, feel free to send and receive exactly the type of JSON objects you want--just be aware that you're going to have to figure out how to deal with them in Rails if they cross models.
Really, it's much cleaner just to tough it out and duplicate a lot of the model architecture in Backbone, unfortunately.
I think the most magical Backbone thing I've seen so far is those js-association-making libraries listed above. Backbone is still fairly clunky.
There is a gem called 'backbone-rails' or 'rails-backbone' (there was a weird project conflict that made it nebulous) by "codebrew" https://github.com/codebrew/backbone-rails, that will generate some nice coffeescript for you to work with. It's probably the easiest way to go, but not as succinct as you might like, since it is a code generator.
If anyone can illuminate more about this, or completely refute me, please do! I'm in the middle of my Backbone knowledge as well.
P.S. You might not want to rely on Backbone to do validations... : ) Easily hackable. If you're adding them in JS to reduce on the number of bad submits, then you're just going to have to live with duplicating the validations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With