<commit_before>﻿
(function (Backbone, _, $) {

  // SharePoint ListData service
  var LISTDATA_SERVICE = '_vti_bin/ListData.svc'

  // calculate url based on site, list and (optional) id
  var url = function (options) {

    var site = options.site,
        list = options.list,
        id = options.id,

    // remove leading and trailing forward slashes from the site path
        path = site.replace(/^\/+|\/+$/g, ''),
        url = (path ? '/' + path : '') + '/' + LISTDATA_SERVICE + '/' + list +
              (id ? '(' + encodeURIComponent(id) + ')' : '');

    return url;
  }


  Backbone.SP = {};



  Backbone.SP.Item = Backbone.Model.extend({

    // the id attribute of a SharePoint item. Please note capital I
    idAttribute: 'Id',

    // the SharePoint site on the current server. By default: root
    site: '',

    initialize: function () {
      this._changeSet = {};
      this.bind('change', this._updateChangeSet)
    },

    _updateChangeSet: function (model) {
      _.extend(this._changeSet, this.changedAttributes());
    },

    parse: function (response) {
      return (response ? response.d : null)
    },

    url: function () {

      var options = {
        site: this.site,
        list: this.list
      }
      if (!this.isNew())
        options.id = this.id;

      return url(options)
    },

    unset: function (attr, options) {
      var result = Backbone.Model.prototype.unset.call(this, attr, options)
      if (result) {
        delete this._changeSet[attr];
      }
      return result;
    },

    clear: function (attr, options) {
      var result = Backbone.Model.prototype.clear.call(this, attr, options)
      if (result) {
        this._changeSet = {};
      }
      return result;
    },



    sync: function (method, model, options) {
      var metadata = model.get("__metadata");

      var methodMap = {
        'create': 'POST',
        'update': 'MERGE',  // OData requires MERGE for partial updates
        'delete': 'DELETE',
        'read': 'GET'
      };

      var type = methodMap[method];

      // Default JSON-request options.
      var params = _.extend({
        type: type,
        dataType: 'json',
        processData: (type == 'GET')
      }, options);

      // Ensure that we have a URL.
      if (!params.url) {
        params.url = model.url();
      }

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

        if (method == 'create') {
          params.data = JSON.stringify(model.toJSON());
        }

        if (method == 'update') {
          params.data = JSON.stringify(model._changeSet || {})
          params.headers = {
            'If-Match': metadata ? metadata.etag : '*'
          }
        }

      }

      // transfer special url parameters like select and orderby to the params.data hash
      if (method == 'read') {
        params.data = params.data || {};
        _(['filter', 'select', 'orderby', 'top', 'skip', 'expand', 'inlinecount']).each(function (keyword) {
          if (options[keyword]) {
            params.data['$' + keyword] = options[keyword]
          }
        })

      }

      // create a success handler to: 
      // - clear the _changeSet after successful sync with server

      var success = params.success;
      params.success = function (resp, status, xhr) {
        // first proecess the response ...
        if (success) success(resp, status, xhr);
        // ..then empty the _changeSet
        model._changeSet = {};

      };

      // Make the request.
      return $.ajax(params);<commit_msg>In a MERGE operation (update) Odata replies with an updated Etag value in the header. In order to allow multiple set/save operation without a additional fetch this value is update in the __metadata properties.<commit_after>﻿
(function (Backbone, _, $) {

  // SharePoint ListData service
  var LISTDATA_SERVICE = '_vti_bin/ListData.svc'

  // calculate url based on site, list and (optional) id
  var url = function (options) {

    var site = options.site,
        list = options.list,
        id = options.id,

    // remove leading and trailing forward slashes from the site path
        path = site.replace(/^\/+|\/+$/g, ''),
        url = (path ? '/' + path : '') + '/' + LISTDATA_SERVICE + '/' + list +
              (id ? '(' + encodeURIComponent(id) + ')' : '');

    return url;
  }


  Backbone.SP = {};



  Backbone.SP.Item = Backbone.Model.extend({

    // the id attribute of a SharePoint item. Please note capital I
    idAttribute: 'Id',

    // the SharePoint site on the current server. By default: root
    site: '',

    initialize: function () {
      this._changeSet = {};
      this.bind('change', this._updateChangeSet)
    },

    _updateChangeSet: function (model) {
      _.extend(this._changeSet, this.changedAttributes());
    },

    parse: function (response) {
      return (response ? response.d : null)
    },

    url: function () {

      var options = {
        site: this.site,
        list: this.list
      }
      if (!this.isNew())
        options.id = this.id;

      return url(options)
    },

    unset: function (attr, options) {
      var result = Backbone.Model.prototype.unset.call(this, attr, options)
      if (result) {
        delete this._changeSet[attr];
      }
      return result;
    },

    clear: function (attr, options) {
      var result = Backbone.Model.prototype.clear.call(this, attr, options)
      if (result) {
        this._changeSet = {};
      }
      return result;
    },



    sync: function (method, model, options) {
      var metadata = model.get("__metadata");

      var methodMap = {
        'create': 'POST',
        'update': 'MERGE',  // OData requires MERGE for partial updates
        'delete': 'DELETE',
        'read': 'GET'
      };

      var type = methodMap[method];

      // Default JSON-request options.
      var params = _.extend({
        type: type,
        dataType: 'json',
        processData: (type == 'GET')
      }, options);

      // Ensure that we have a URL.
      if (!params.url) {
        params.url = model.url();
      }

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

        if (method == 'create') {
          params.data = JSON.stringify(model.toJSON());
        }

        if (method == 'update') {
          params.data = JSON.stringify(model._changeSet || {})
          params.headers = {
            'If-Match': metadata ? metadata.etag : '*'
          }
        }

      }

      // transfer special url parameters like select and orderby to the params.data hash
      if (method == 'read') {
        params.data = params.data || {};
        _(['filter', 'select', 'orderby', 'top', 'skip', 'expand', 'inlinecount']).each(function (keyword) {
          if (options[keyword]) {
            params.data['$' + keyword] = options[keyword]
          }
        })

      }

      // create a success handler to: 
      // - clear the _changeSet after successful sync with server

      var success = params.success;
      params.success = function (resp, status, xhr) {
          // first process the response ...
           if (success) {
               // OData responds with an updated Etag
               var etag = xhr.getResponseHeader('Etag');
               success(resp, status, xhr);
               if (etag) {
                   // Backbone doesn't support setting/getting nested attributes
                   // Updating etag attribute directly instead
                   model.attributes.__metadata.etag = etag;
               }
           }
           // ..then empty the _changeSet
           model._changeSet = {};
      };

      // Make the request.
      return $.ajax(params);
{'fraction_non_alphanumeric': 0.13645544054531683, 'fraction_numerical': 0.0, 'mean_word_length': 1.9344444444444444, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4973403', 'n_tokens_mistral': 2293, 'n_tokens_neox': 2216, 'n_words': 634}