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

    https://github.com/apache/couchdb-fauxton/pull/204#discussion_r22274363
  
    --- Diff: app/addons/documents/views-doceditor.js ---
    @@ -398,77 +308,89 @@ function (app, FauxtonAPI, Components, Documents, 
Databases, prettify) {
                 msg: 'Save failed: ' + responseText,
                 type: 'error',
                 fade: false,
    -            clear: true,
    -            selector: '#doc .errors-container'
    +            clear: true
               });
             });
           } else if(this.model.validationError && this.model.validationError 
=== 'Cannot change a documents id.') {
    -          FauxtonAPI.addNotification({
    -            msg: 'Cannot save: ' + 'Cannot change a documents _id, try 
Duplicate doc instead!',
    -            type: 'error',
    -            selector: '#doc .errors-container',
    -            clear:  true
    -          });
    +        FauxtonAPI.addNotification({
    +          msg: 'Cannot save. Cannot change a documents _id, try Clone 
Document instead!',
    +          type: 'error',
    +          clear:  true
    +        });
             delete this.model.validationError;
           } else {
             FauxtonAPI.addNotification({
    -          msg: 'Please fix the JSON errors and try again.',
    +          msg: 'Please fix the JSON errors and try saving again.',
               type: 'error',
    -          selector: '#doc .errors-container',
               clear:  true
             });
           }
         },
     
         getDocFromEditor: function () {
    -      var json;
    -
           if (!this.hasValidCode()) {
             return false;
           }
    -
    -      json = JSON.parse(this.editor.getValue());
    -
    +      var json = JSON.parse(this.editor.getValue());
           this.model.clear().set(json, {validate: true});
           if (this.model.validationError) {
             return false;
           }
    -
           return this.model;
         },
     
    -    hasValidCode: function () {
    -      var errors = this.editor.getAnnotations();
    -      return errors.length === 0;
    +    beforeRender: function () {
    +      this.uploadModal = this.setView('#upload-modal', new 
this.components.uploadModal({ model: this.model }));
    +      this.duplicateModal = this.setView('#duplicate-modal', new 
this.components.duplicateDocModal({ model: this.model }));
    +      this.confirmDeleteModal = this.setView('#delete-doc-modal', new 
this.components.confirmModal({
    +        text: 'Are you sure you want to delete this document?',
    +        action: this.deleteDocument
    +      }));
    +
    +      // ensures it's initialized only once
    +      this.stringEditModal = this.stringEditModal || 
this.setView('#string-edit-modal', new Views.StringEditModal());
         },
     
    -    serialize: function () {
    -      return {
    -        doc: this.model,
    -        attachments: this.getAttachments()
    -      };
    +    updateValues: function () {
    +      if (this.model.changedAttributes()) {
    +        FauxtonAPI.addNotification({
    +          msg: 'Document saved successfully.',
    +          type: 'success',
    +          clear: true
    +        });
    +        this.editor.setValue(this.model.prettyJSON());
    +      }
         },
     
    -    getAttachments: function () {
    -      var attachments = this.model.get('_attachments');
    +    establish: function () {
    +      var promise = this.model.fetch(),
    +          deferred = $.Deferred(),
    +          that = this;
     
    -      if (!attachments) { return false; }
    +      promise.then(function () {
    +        deferred.resolve();
    +      }, function (xhr, reason, msg) {
    +        if (xhr.status === 404) {
    +          FauxtonAPI.addNotification({
    +            msg: 'The document does not exist',
    +            type: 'error',
    +            clear: true
    +          });
    +          that.goBack();
    +        }
    +        deferred.reject();
    +     });
     
    -      return _.map(attachments, function (att, key) {
    -        return {
    -          fileName: key,
    -          size: att.length,
    -          contentType: att.content_type,
    -          url: this.model.url() + '/' + app.utils.safeURLName(key)
    -        };
    -      }, this);
    +      return deferred;
         },
     
    -    afterRender: function () {
    -      var saveDoc = this.saveDoc,
    -          editor,
    -          model;
    +    hasValidCode: function () {
    +      var errors = this.editor.getAnnotations();
    +      return errors.length === 0;
    +    },
     
    +    afterRender: function () {
    +      _.bind(this.saveDoc, this);
    --- End diff --
    
    that is not how binding context with `this` works
    
    You can either use the native way to bind the context of this:
    
    ```javascript
              exec: function (editor) {
                this.saveDoc();
              }.bind(this),
    ```
    
    or use underscore/lodash (http://underscorejs.org/#bind):
    
    ```javascript
          var saveDoc = _.bind(this.saveDoc, this);
          this.listenTo(this.model, 'sync', this.updateValues);
    
          this.editor = new Components.Editor({
            editorId: 'editor-container',
            forceMissingId: true,
            commands: [{
              name: 'save',
              bindKey: {win: 'Ctrl-S',  mac: 'Ctrl-S'},
              exec: function (editor) {
                saveDoc();
              },
              readOnly: true // false if this command should not apply in 
readOnly mode
            }]
          });
    ```


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

Reply via email to