Skip to content

Instantly share code, notes, and snippets.

@somebox
Forked from trydionel/backbone.rails.js
Created June 30, 2011 10:42
Show Gist options
  • Select an option

  • Save somebox/1055990 to your computer and use it in GitHub Desktop.

Select an option

Save somebox/1055990 to your computer and use it in GitHub Desktop.

Revisions

  1. somebox renamed this gist Jun 30, 2011. 1 changed file with 8 additions and 1 deletion.
    9 changes: 8 additions & 1 deletion public/javascripts/backbone.rails.js → backbone.rails.js
    Original file line number Diff line number Diff line change
    @@ -9,6 +9,8 @@
    //
    // Load this file after backbone.js and before your application JS.
    //
    // Modified to add Rails3 CSRF support!


    Backbone.RailsJSON = {
    // In order to properly wrap/unwrap Rails JSON data, we need to specify
    @@ -57,7 +59,12 @@ _.extend(Backbone.Model.prototype, Backbone.RailsJSON, {
    // This is called just before a model is persisted to a remote server.
    // Wraps the model's attributes into a Rails-friendly format.
    toJSON : function() {
    return this.wrappedAttributes();
    // hack in rails auth token
    var object = this.wrappedAttributes();
    var csrfName = $("meta[name='csrf-param']").attr('content');
    var csrfValue = $("meta[name='csrf-token']").attr('content');
    object[csrfName] = csrfValue;
    return object;
    },

    // A new default initializer which handles data directly from Rails as
  2. @trydionel trydionel created this gist Nov 28, 2010.
    68 changes: 68 additions & 0 deletions public/javascripts/backbone.rails.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    //
    // Backbone.Rails.js
    //
    // Makes Backbone.js play nicely with the default Rails setup, i.e.,
    // no need to set
    // ActiveRecord::Base.include_root_in_json = false
    // and build all of your models directly from `params` rather than
    // `params[:model]`.
    //
    // Load this file after backbone.js and before your application JS.
    //

    Backbone.RailsJSON = {
    // In order to properly wrap/unwrap Rails JSON data, we need to specify
    // what key the object will be wrapped with.
    _name : function() {
    if (!this.name) throw new Error("A 'name' property must be specified");
    return this.name;
    },

    // A test to indicate whether the given object is wrapped.
    isWrapped : function(object) {
    return (object.hasOwnProperty(this._name()) &&
    (typeof(object[this._name()]) == "object"));
    },

    // Extracts the object's wrapped attributes.
    unwrappedAttributes : function(object) {
    return object[this._name()];
    },

    // Wraps the model's attributes under the supplied key.
    wrappedAttributes : function() {
    var object = new Object;
    object[this._name()] = _.clone(this.attributes);
    return object;
    },

    // Sets up the new model's internal state so that it matches the
    // expected format. Should be called early in the model's constructor.
    maybeUnwrap : function(args) {
    if (this.isWrapped(args)) {
    this.set(this.unwrappedAttributes(args), { silent: true });
    this.unset(this._name(), { silent: true });
    this._previousAttributes = _.clone(this.attributes);
    }
    }
    };

    _.extend(Backbone.Model.prototype, Backbone.RailsJSON, {
    // This is called on all models coming in from a remote server.
    // Unwraps the given response from the default Rails format.
    parse : function(resp) {
    return this.unwrappedAttributes(resp);
    },

    // This is called just before a model is persisted to a remote server.
    // Wraps the model's attributes into a Rails-friendly format.
    toJSON : function() {
    return this.wrappedAttributes();
    },

    // A new default initializer which handles data directly from Rails as
    // well as unnested data.
    initialize : function(args) {
    this.maybeUnwrap(args);
    }
    });