Github user garrensmith commented on a diff in the pull request:

    https://github.com/apache/couchdb/pull/221#discussion_r12520595
  
    --- Diff: src/fauxton/app/addons/documents/resources.js ---
    @@ -295,6 +295,120 @@ function(app, FauxtonAPI, PagingCollection) {
     
       });
     
    +  Documents.BulkDeleteDoc = FauxtonAPI.Model.extend({
    +    idAttribute: "_id"
    +  });
    +
    +  Documents.BulkDeleteDocCollection = FauxtonAPI.Collection.extend({
    +
    +    model: Documents.BulkDeleteDoc,
    +
    +    initialize: function (models, options) {
    +      this.databaseId = options.databaseId;
    +    },
    +
    +    bulkDelete: function () {
    +      var payload = this.createPayload(this.toJSON()),
    +          that = this;
    +
    +      $.ajax({
    +        type: 'POST',
    +        url: app.host + '/' + this.databaseId + '/_bulk_docs',
    +        contentType: 'application/json',
    +        dataType: 'json',
    +        data: JSON.stringify(payload),
    +      })
    +      .then(function (res) {
    +        that.handleResponse(res);
    +      })
    +      .fail(function () {
    +        var ids = _.reduce(this.toArray(), function (acc, doc) {
    +          acc.push(doc.id);
    +          return acc;
    +        }, []);
    +        that.trigger('error', ids);
    +      });
    +    },
    +
    +    handleResponse: function (res) {
    +      var errorIds = [],
    +          successIds = [],
    +          doc;
    +
    +      for (doc in res) {
    +        if (res[doc].error) {
    +          errorIds.push(res[doc].id);
    +        }
    +        if (res[doc].ok === true) {
    +          successIds.push(res[doc].id);
    +        }
    +      }
    +
    +      this.removeDocuments(successIds);
    +
    +      if (!errorIds.length) {
    +        this.clear();
    +      } else {
    +        this.trigger('error', errorIds);
    +        this.save();
    +      }
    +
    +      this.trigger('updated');
    +    },
    +
    +    removeDocuments: function (ids) {
    +      _.each(ids, function (id) {
    +        if (/_design/.test(id)) {
    +          FauxtonAPI.triggerRouteEvent('reloadDesignDocs');
    +        }
    +
    +        this.remove(this.get(id));
    +      }, this);
    +
    +      this.trigger('removed', ids);
    +    },
    +
    +    createList: function (documents) {
    +      var documentList = [],
    +          id;
    +
    +      for (id in documents) {
    +        documentList.push(documents[id]);
    +      }
    +
    +      return documentList;
    +    },
    +
    +    createPayload: function (documents) {
    +      var documentList = this.createList(documents);
    +
    +      return {
    +        docs: documentList
    +      };
    +    },
    +
    +    parse: function (resp) {
    +      return this.createList(resp);
    +    },
    +
    +    clear: function () {
    +      window.sessionStorage.removeItem('couchdb:docsToDelete:' + 
this.databaseId);
    +    },
    +
    +    save: function () {
    +      var data = JSON.stringify(this.toJSON());
    --- End diff --
    
    I'm a little confused. Why do we need to keep a list in storage of the docs 
to delete. Is this show it works across pagination?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to