Usergrid.Entity.prototype.save = function()

in portal/js/libs/usergrid.sdk.js [1211:1282]


  Usergrid.Entity.prototype.save = function(callback) {
    var type = this.get('type');
    var method = 'POST';
    if (isUUID(this.get('uuid'))) {
      method = 'PUT';
      type += '/' + this.get('uuid');
    }

    //update the entity
    var self = this;
    var data = {};
    var entityData = this.get();
    //remove system specific properties
    for (var item in entityData) {
      if (item === 'metadata' || item === 'created' || item === 'modified' ||
        item === 'type' || item === 'activated' || item === 'uuid') {
        continue;
      }
      data[item] = entityData[item];
    }
    var options = {
      method: method,
      endpoint: type,
      body: data
    };
    //save the entity first
    this._client.request(options, function(err, retdata) {
      if (err && self._client.logging) {
        console.log('could not save entity');
        if (typeof(callback) === 'function') {
          return callback(err, retdata, self);
        }
      } else {
        if (retdata.entities) {
          if (retdata.entities.length) {
            var entity = retdata.entities[0];
            self.set(entity);
            //for connections, API returns type
            self.set('type', retdata.path);
          }
        }
        //if this is a user, update the password if it has been specified;
        var needPasswordChange = (self.get('type') === 'user' && entityData
          .oldpassword && entityData.newpassword);
        if (needPasswordChange) {
          //Note: we have a ticket in to change PUT calls to /users to accept the password change
          //      once that is done, we will remove this call and merge it all into one
          var pwdata = {};
          pwdata.oldpassword = entityData.oldpassword;
          pwdata.newpassword = entityData.newpassword;
          var options = {
            method: 'PUT',
            endpoint: type + '/password',
            body: pwdata
          }
          self._client.request(options, function(err, data) {
            if (err && self._client.logging) {
              console.log('could not update user');
            }
            //remove old and new password fields so they don't end up as part of the entity object
            self.set('oldpassword', null);
            self.set('newpassword', null);
            if (typeof(callback) === 'function') {
              callback(err, data, self);
            }
          });
        } else if (typeof(callback) === 'function') {
          callback(err, retdata, self);
        }
      }
    });
  }