1

I have been hearing a lot about Backbone and wanted to use it for my latest project to learn it. However, I am coming from Rails background, and my experiences do not seem to translate well for Backbone.

For example, I have four models which need to be displayed in a product view.

class Product < ActiveRecord::Base
  belongs_to :category
  belongs_to :child
  has_many :actions
  has_many :comments
end

class Child < ActiveRecord::Base
  belongs_to :user
  has_many :products
end

I am using Backbone-relational to define relationships in Backbone models. The following is product Backbone model.

class Ibabybox.Models.Product extends Backbone.RelationalModel
  paramRoot: 'product'
  urlRoot: '/products'

  relations: 
    [
      {
        type: Backbone.HasMany
        key: 'actions'
        relatedModel: 'Ibabybox.Models.Action'
        collectionType: 'Ibabybox.Collections.ActionsCollection'
        reverseRelation:
          key: 'product'
          includeInJSON: 'id'
      } 
    ]

In the Backbone router, I do the following.

class Ibabybox.Routers.ProductsRouter extends Backbone.Router

routes:
"": "index"
":id": "show"

show: (id) ->
  @product = new Ibabybox.Models.Product({id: id})
  @product.fetch
    success: (product) ->
      actions = product.get('actions')
      child = product.get('child')
      @child_model = new Ibabybox.Models.Child({id: child.id})
      user = @child_model.get('user')
      @view = new Ibabybox.Views.Products.ShowView({model: product, actions: actions, child: child, user: user})
      $("#products").html(@view.render().el)

And on Rails controller, I do the following.

class ProductsController < ApplicationController
  def show
    @product = Product.find(params[:id])
    render json: @product.to_json(:include => [ :actions, 
                                            :child => { :include => {:user => {:methods => [:name] }}} ])
  end
end

First, am I going about this in the right direction?

Second, it feels like a lot of things to define and write to display related things for a product and the reason for feeling I might be doing something wrong....

Any help/recommendation/correction would be much much appreciated!

Thanks!

1

1 Answer 1

1

According to backbone.js doc There's More Than One Way To Do It, but some ways become pain while your application grows. After developing 3 application using backbone.js & rails here is my experience.

  1. backbone.js is not suited for every website. Specially if your website is content based. In this case using pjax give better result.

  2. Resource based design is best approach. And hopefully both of rails and backbone.js rock. You can easily use default rails generator with minimal changes. For complicated objects i prefer using jbuilder since i can cache objects.

  3. If you are using explicit $.ajax,$.post,$.get probably you are wrong.[same as 2]

  4. If you are repeating yourself there is something wrong.

  5. Don't miss backbone events.( In your case you can use events instead of passing success callback to fetch)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.