Okey, so I decided to go with backbone.js in my rails app, but I feel that I'm doing a lot of redundant coding. I'm having a lot of models and templates in backbone that are more or less the same as their rails equivalent. Is there some kind of gem that handles this? Something like act_as_backbonemodel that you put in to your rails model that generates the backbone-model (with possibility to be overridden to add functionality). Also it would be great if there existed some kind of rails-backbone-view gem that lets you put your templates in your views directory and use normal partials/reuse normal rails views.
The closest Gem I know of is: https://github.com/codebrew/backbone-rails Perhaps in combination with: https://github.com/netzpirat/haml_coffee_assets
Having the Backbone templates in app/views doesn't make sense, since these should be lean JS (or compiled to js) files that contain (almost) no logic. You should consider loading them from a CDN through the asset pipeline.
A common use case is to let Backbone handle the front data logic and use Rails as just your API.
When I started using Backbone with Rails (almost a year ago), I also wanted a gem like this (I used backbone-rails at the time). Initially it seemed like many things were duplicated between backbone and rails, but as development progressed, I found this to be the exception rather than the rule. Especially if you're using Rails as an API, this kind of gem may only be helpful at the start of a project, and less relevant afterwards.
I say this because most of our models no longer have a 1-1 correspondence with our Rails resources. For example, while information on people is required, this information comes from a combination of a number of resources, and excludes much of the data in the primary people records. Also, creating and modifying them isn't appropriate in our app, and creation is by invitation only, and that's exposed as a different resource entirely.
Other resources aren't needed, and yet others have such a small subset of functionality or data, or have data from multiple resources, that having something generate them really wouldn't work.
Since rails is the API in our case, we don't have any duplication in the view layer.
I find that creating a custom generators that match the way you work with backbone, or having a few templates in your editor of choice to be a more flexible solution.
In terms of organization, we've added backbone as a directory under the app/assets/javascripts directory, and created directories for models, views, routers, helpers, etc. here. This allows the use of coffeescript to write backbone without any gems needed. We use the jasmine and jasminerice gems to test our backbone code with coffeescript and haml for fixtures (these live in the spec/javascripts directory}.
As for the templates, they are all rails view partials in our controllers that get rendered into the header. Backbone looks them up by id and uses them from there.
Hope this helps.
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