BaseSettings.js

From disqus.com, 7 Months ago, written in JavaScript, viewed 3 times. This paste is a reply to withLoginOverlay.js from disqus.com - view diff
URL https://pastebin.freepbx.org/view/f19c8515 Embed
Download Paste or View Raw
  1. define('home/models/common/BaseSettings',[
  2.     'underscore',
  3.     'backbone',
  4.  
  5.     'home/models/Session',
  6. ], function (
  7.     _,
  8.     Backbone,
  9.  
  10.     Session
  11. ) {
  12.     'use strict';
  13.  
  14.     var BaseSettings = Backbone.Model.extend({
  15.         shouldFetch: function () {
  16.             return !this.fetched && !this.fetching;
  17.         },
  18.  
  19.         ensureFetched: function () {
  20.             if (!this.shouldFetch())
  21.                 return;
  22.  
  23.             var self = this;
  24.             self.fetching = true;
  25.             self.fetch({
  26.                 success: function () {
  27.                     self.fetched = true;
  28.                     self.fetching = false;
  29.                 },
  30.                 error: function () {
  31.                     self.fetched = false;
  32.                     self.fetching = false;
  33.                 },
  34.             });
  35.         },
  36.  
  37.         toFormData: function (attrs) {
  38.             attrs = attrs || this.toJSON();
  39.             return _.reduce(attrs, function (formData, value, key) {
  40.                 formData.append(key, _.isString(value) ? value.trim() : value);
  41.                 return formData;
  42.             }, new window.FormData());
  43.         },
  44.  
  45.         // set to true if form has a file upload input field
  46.         useMultiPartForm: false,
  47.  
  48.         save: function (attrs, options) {
  49.             attrs = attrs || this.toJSON();
  50.             options = options || {};
  51.             if (this.useMultiPartForm) {
  52.                 options.data = this.toFormData(attrs);
  53.                 options.contentType = false;
  54.             }
  55.             options.beforeSend = this.setCSRFHeader;
  56.             return Backbone.Model.prototype.save.call(this, attrs, options);
  57.         },
  58.  
  59.         setCSRFHeader: function (xhr, settings) {
  60.             var csrftoken = Session.get().getCsrfToken();
  61.             if (!/^(GET|HEAD|OPTIONS|TRACE)$/.test(settings.type))
  62.                 xhr.setRequestHeader('X-CSRFToken', csrftoken);
  63.         },
  64.  
  65.         destroy: function (options) {
  66.             options = options || {};
  67.             options.beforeSend = this.setCSRFHeader;
  68.             return Backbone.Model.prototype.destroy.call(this, options);
  69.         },
  70.     });
  71.  
  72.     return BaseSettings;
  73. });
  74.  
  75.  
  76. // https://c.disquscdn.com/next/82c6de3/home/js/models/common/BaseSettings.js

Replies to BaseSettings.js rss

Title Name Language When
Re: BaseSettings.js Flying Cockroach javascript 3 Months ago.
confirmBlacklistUser.js disqus.com javascript 7 Months ago.

Reply to "BaseSettings.js"

Here you can reply to the paste above