Search code examples
ruby-on-railsbackbone.jsbackbone-rails

Backbone.js: What is paramRoot: attribute in backbone.model?


I use rails-backbone gem and I generated a Backbone model. The model includes paramRoot: attribute. I assume it somehow tells Backbone how to connect to the corresponding Rails model, but I can't find any documentation about it.

What does paramRoot actually do?


Solution

  • Backbone-rails doesn't document paramRoot. I suppose you're supposed to use the generators to build your models:

    class <%= model_namespace %> extends Backbone.Model
      paramRoot: '<%= singular_table_name %>'
      #...
    

    and blindly do as you're told.

    If you want to know what it does then you have to read the source (as usual). The only thing in Backbone-rails that uses paramRoot is their replacement for the standard Backbone.sync; their replacement contains this:

    if(model.paramRoot) {
      data[model.paramRoot] = model.toJSON();
    } else {
      data = model.toJSON();
    }
    

    All that does is change a model's serialized attributes from the standard {attr1: v1, attr2: v2, ...} Backbone form to the { model_name: { attr1: v1, ... } } form that Rails wants; then you can look at params[:model_name] in your Rails controllers rather than looking at just params.