Sha256: 05f2b991672b5f9c7f7df5b09c3b549a035e05817d283f91d3daf01195039fdb

Contents?: true

Size: 1.42 KB

Versions: 4

Compression:

Stored size: 1.42 KB

Contents

(function() {
  var methodMap = {
    'create': 'POST',
    'update': 'PUT',
    'delete': 'DELETE',
    'read'  : 'GET'
  };
  
  var getUrl = function(object) {
    if (!(object && object.url)) return null;
    return _.isFunction(object.url) ? object.url() : object.url;
  };
  
  var urlError = function() {
    throw new Error("A 'url' property or function must be specified");
  };

  Backbone.sync = function(method, model, options) {
    var type = methodMap[method];

    // Default JSON-request options.
    var params = _.extend({
      type:         type,
      dataType:     'json',
      beforeSend: function( xhr ) {
        var token = $('meta[name="csrf-token"]').attr('content');
        if (token) xhr.setRequestHeader('X-CSRF-Token', token);
      }
    }, options);

    if (!params.url) {
      params.url = getUrl(model) || urlError();
    }

    // Ensure that we have the appropriate request data.
    if (!params.data && model && (method == 'create' || method == 'update')) {
      params.contentType = 'application/json';

      var data = {}

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

      params.data = JSON.stringify(data)
    }

    // Don't process data on a non-GET request.
    if (params.type !== 'GET') {
      params.processData = false;
    }
    
    // Make the request.
    return $.ajax(params);
  }
  
}).call(this);

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
rails-backbone-sp-0.0.1 vendor/assets/javascripts/backbone_rails_sync.js
rails-backbone-0.5.4 vendor/assets/javascripts/backbone_rails_sync.js
rails-backbone-0.5.3 vendor/assets/javascripts/backbone_rails_sync.js
rails-backbone-0.5.2 vendor/assets/javascripts/backbone_rails_sync.js