You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@usergrid.apache.org by sn...@apache.org on 2014/01/27 23:21:03 UTC
[17/27] built with the new Events module included
http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/8367ea49/sdks/html5-javascript/usergrid.js
----------------------------------------------------------------------
diff --git a/sdks/html5-javascript/usergrid.js b/sdks/html5-javascript/usergrid.js
old mode 100755
new mode 100644
index 948238b..96dbbe8
--- a/sdks/html5-javascript/usergrid.js
+++ b/sdks/html5-javascript/usergrid.js
@@ -1,3 +1,4 @@
+/*! usergrid@0.0.0 2014-01-16 */
/*
* This module is a collection of classes designed to make working with
* the Appigee App Services API as easy as possible.
@@ -21,37 +22,89 @@
* @author matt dobson (matt@apigee.com)
* @author ryan bridges (rbridges@apigee.com)
*/
-
-
//Hack around IE console.log
window.console = window.console || {};
+
window.console.log = window.console.log || function() {};
//Usergrid namespace encapsulates this SDK
window.Usergrid = window.Usergrid || {};
+
Usergrid = Usergrid || {};
-Usergrid.USERGRID_SDK_VERSION = '0.10.07';
-Usergrid.Client = function(options) {
- //usergrid enpoint
- this.URI = options.URI || 'https://api.usergrid.com';
+Usergrid.USERGRID_SDK_VERSION = "0.10.07";
- //Find your Orgname and Appname in the Admin portal (http://apigee.com/usergrid)
- if (options.orgName) {
- this.set('orgName', options.orgName);
- }
- if (options.appName) {
- this.set('appName', options.appName);
- }
+/*
+ * Tests if the string is a uuid
+ *
+ * @public
+ * @method isUUID
+ * @param {string} uuid The string to test
+ * @returns {Boolean} true if string is uuid
+ */
+function isUUID(uuid) {
+ var uuidValueRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/;
+ if (!uuid) {
+ return false;
+ }
+ return uuidValueRegex.test(uuid);
+}
- //other options
- this.buildCurl = options.buildCurl || false;
- this.logging = options.logging || false;
+/*
+ * method to encode the query string parameters
+ *
+ * @method encodeParams
+ * @public
+ * @params {object} params - an object of name value pairs that will be urlencoded
+ * @return {string} Returns the encoded string
+ */
+function encodeParams(params) {
+ var tail = [];
+ var item = [];
+ var i;
+ if (params instanceof Array) {
+ for (i in params) {
+ item = params[i];
+ if (item instanceof Array && item.length > 1) {
+ tail.push(item[0] + "=" + encodeURIComponent(item[1]));
+ }
+ }
+ } else {
+ for (var key in params) {
+ if (params.hasOwnProperty(key)) {
+ var value = params[key];
+ if (value instanceof Array) {
+ for (i in value) {
+ item = value[i];
+ tail.push(key + "=" + encodeURIComponent(item));
+ }
+ } else {
+ tail.push(key + "=" + encodeURIComponent(value));
+ }
+ }
+ }
+ }
+ return tail.join("&");
+}
- //timeout and callbacks
- this._callTimeout = options.callTimeout || 30000; //default to 30 seconds
- this._callTimeoutCallback = options.callTimeoutCallback || null;
- this.logoutCallback = options.logoutCallback || null;
+Usergrid.Client = function(options) {
+ //usergrid enpoint
+ this.URI = options.URI || "https://api.usergrid.com";
+ //Find your Orgname and Appname in the Admin portal (http://apigee.com/usergrid)
+ if (options.orgName) {
+ this.set("orgName", options.orgName);
+ }
+ if (options.appName) {
+ this.set("appName", options.appName);
+ }
+ //other options
+ this.buildCurl = options.buildCurl || false;
+ this.logging = options.logging || false;
+ //timeout and callbacks
+ this._callTimeout = options.callTimeout || 3e4;
+ //default to 30 seconds
+ this._callTimeoutCallback = options.callTimeoutCallback || null;
+ this.logoutCallback = options.logoutCallback || null;
};
/*
@@ -70,139 +123,125 @@ Usergrid.Client = function(options) {
* @param {function} callback
* @return {callback} callback(err, data)
*/
-Usergrid.Client.prototype.request = function (options, callback) {
- var self = this;
- var method = options.method || 'GET';
- var endpoint = options.endpoint;
- var body = options.body || {};
- var qs = options.qs || {};
- var mQuery = options.mQuery || false; //is this a query to the management endpoint?
- var orgName = this.get('orgName');
- var appName = this.get('appName');
- if(!mQuery && !orgName && !appName){
- if (typeof(this.logoutCallback) === 'function') {
- return this.logoutCallback(true, 'no_org_or_app_name_specified');
- }
- }
- if (mQuery) {
- uri = this.URI + '/' + endpoint;
- } else {
- uri = this.URI + '/' + orgName + '/' + appName + '/' + endpoint;
- }
-
- if (self.getToken()) {
- qs.access_token = self.getToken();
- /* //could also use headers for the token
- xhr.setRequestHeader("Authorization", "Bearer " + self.getToken());
- xhr.withCredentials = true;
- */
- }
-
- //append params to the path
- var encoded_params = encodeParams(qs);
- if (encoded_params) {
- uri += "?" + encoded_params;
- }
-
- //stringify the body object
- body = JSON.stringify(body);
-
- //so far so good, so run the query
- var xhr = new XMLHttpRequest();
- xhr.open(method, uri, true);
- //add content type = json if there is a json payload
- if (body) {
- xhr.setRequestHeader("Content-Type", "application/json");
- xhr.setRequestHeader("Accept", "application/json");
- }
-
- // Handle response.
- xhr.onerror = function(response) {
- self._end = new Date().getTime();
- if (self.logging) {
- console.log('success (time: ' + self.calcTimeDiff() + '): ' + method + ' ' + uri);
- }
- if (self.logging) {
- console.log('Error: API call failed at the network level.');
- }
- //network error
- clearTimeout(timeout);
- var err = true;
- if (typeof(callback) === 'function') {
- callback(err, response);
- }
- };
-
- xhr.onload = function(response) {
- //call timing, get time, then log the call
- self._end = new Date().getTime();
- if (self.logging) {
- console.log('success (time: ' + self.calcTimeDiff() + '): ' + method + ' ' + uri);
- }
- //call completed
- clearTimeout(timeout);
- //decode the response
- try{
- response = JSON.parse(xhr.responseText);
- }catch (e){
- response = {error:'unhandled_error',error_description:xhr.responseText};
- xhr.status = xhr.status === 200 ? 400 : xhr.status;
- console.error(e);
+Usergrid.Client.prototype.request = function(options, callback) {
+ var self = this;
+ var method = options.method || "GET";
+ var endpoint = options.endpoint;
+ var body = options.body || {};
+ var qs = options.qs || {};
+ var mQuery = options.mQuery || false;
+ //is this a query to the management endpoint?
+ var orgName = this.get("orgName");
+ var appName = this.get("appName");
+ if (!mQuery && !orgName && !appName) {
+ if (typeof this.logoutCallback === "function") {
+ return this.logoutCallback(true, "no_org_or_app_name_specified");
+ }
}
- if (xhr.status != 200) {
- //there was an api error
- var error = response.error;
- var error_description = response.error_description;
- if (self.logging) {
- console.log('Error (' + xhr.status + ')(' + error + '): ' + error_description);
- }
- if ( (error == "auth_expired_session_token") ||
- (error == "auth_missing_credentials") ||
- (error == "auth_unverified_oath") ||
- (error == "expired_token") ||
- (error == "unauthorized") ||
- (error == "auth_invalid")) {
- //these errors mean the user is not authorized for whatever reason. If a logout function is defined, call it
- //if the user has specified a logout callback:
- if (typeof(self.logoutCallback) === 'function') {
- return self.logoutCallback(true, response);
- }
- }
- if (typeof(callback) === 'function') {
- callback(true, response);
- }
+ if (mQuery) {
+ uri = this.URI + "/" + endpoint;
} else {
- if (typeof(callback) === 'function') {
- callback(false, response);
- }
- }
- };
-
- var timeout = setTimeout(
- function() {
- xhr.abort();
- if (self._callTimeoutCallback === 'function') {
- self._callTimeoutCallback('API CALL TIMEOUT');
- } else {
- self.callback('API CALL TIMEOUT');
- }
- },
- self._callTimeout); //set for 30 seconds
-
- if (this.logging) {
- console.log('calling: ' + method + ' ' + uri);
- }
- if (this.buildCurl) {
- var curlOptions = {
- uri:uri,
- body:body,
- method:method
+ uri = this.URI + "/" + orgName + "/" + appName + "/" + endpoint;
+ }
+ if (self.getToken()) {
+ qs.access_token = self.getToken();
+ }
+ //append params to the path
+ var encoded_params = encodeParams(qs);
+ if (encoded_params) {
+ uri += "?" + encoded_params;
+ }
+ //stringify the body object
+ body = JSON.stringify(body);
+ //so far so good, so run the query
+ var xhr = new XMLHttpRequest();
+ xhr.open(method, uri, true);
+ //add content type = json if there is a json payload
+ if (body) {
+ xhr.setRequestHeader("Content-Type", "application/json");
+ xhr.setRequestHeader("Accept", "application/json");
+ }
+ // Handle response.
+ xhr.onerror = function(response) {
+ self._end = new Date().getTime();
+ if (self.logging) {
+ console.log("success (time: " + self.calcTimeDiff() + "): " + method + " " + uri);
+ }
+ if (self.logging) {
+ console.log("Error: API call failed at the network level.");
+ }
+ //network error
+ clearTimeout(timeout);
+ var err = true;
+ if (typeof callback === "function") {
+ callback(err, response);
+ }
+ };
+ xhr.onload = function(response) {
+ //call timing, get time, then log the call
+ self._end = new Date().getTime();
+ if (self.logging) {
+ console.log("success (time: " + self.calcTimeDiff() + "): " + method + " " + uri);
+ }
+ //call completed
+ clearTimeout(timeout);
+ //decode the response
+ try {
+ response = JSON.parse(xhr.responseText);
+ } catch (e) {
+ response = {
+ error: "unhandled_error",
+ error_description: xhr.responseText
+ };
+ xhr.status = xhr.status === 200 ? 400 : xhr.status;
+ console.error(e);
+ }
+ if (xhr.status != 200) {
+ //there was an api error
+ var error = response.error;
+ var error_description = response.error_description;
+ if (self.logging) {
+ console.log("Error (" + xhr.status + ")(" + error + "): " + error_description);
+ }
+ if (error == "auth_expired_session_token" || error == "auth_missing_credentials" || error == "auth_unverified_oath" || error == "expired_token" || error == "unauthorized" || error == "auth_invalid") {
+ //these errors mean the user is not authorized for whatever reason. If a logout function is defined, call it
+ //if the user has specified a logout callback:
+ if (typeof self.logoutCallback === "function") {
+ return self.logoutCallback(true, response);
+ }
+ }
+ if (typeof callback === "function") {
+ callback(true, response);
+ }
+ } else {
+ if (typeof callback === "function") {
+ callback(false, response);
+ }
+ }
+ };
+ var timeout = setTimeout(function() {
+ xhr.abort();
+ if (self._callTimeoutCallback === "function") {
+ self._callTimeoutCallback("API CALL TIMEOUT");
+ } else {
+ self.callback("API CALL TIMEOUT");
+ }
+ }, self._callTimeout);
+ //set for 30 seconds
+ if (this.logging) {
+ console.log("calling: " + method + " " + uri);
+ }
+ if (this.buildCurl) {
+ var curlOptions = {
+ uri: uri,
+ body: body,
+ method: method
+ };
+ this.buildCurlCall(curlOptions);
}
- this.buildCurlCall(curlOptions);
- }
- this._start = new Date().getTime();
- xhr.send(body);
-}
+ this._start = new Date().getTime();
+ xhr.send(body);
+};
/*
* function for building asset urls
@@ -213,21 +252,18 @@ Usergrid.Client.prototype.request = function (options, callback) {
* @return {string} assetURL
*/
Usergrid.Client.prototype.buildAssetURL = function(uuid) {
- var self = this;
- var qs = {};
- var assetURL = this.URI + '/' + this.orgName + '/' + this.appName + '/assets/' + uuid + '/data';
-
- if (self.getToken()) {
- qs.access_token = self.getToken();
- }
-
- //append params to the path
- var encoded_params = encodeParams(qs);
- if (encoded_params) {
- assetURL += "?" + encoded_params;
- }
-
- return assetURL;
+ var self = this;
+ var qs = {};
+ var assetURL = this.URI + "/" + this.orgName + "/" + this.appName + "/assets/" + uuid + "/data";
+ if (self.getToken()) {
+ qs.access_token = self.getToken();
+ }
+ //append params to the path
+ var encoded_params = encodeParams(qs);
+ if (encoded_params) {
+ assetURL += "?" + encoded_params;
+ }
+ return assetURL;
};
/*
@@ -240,29 +276,27 @@ Usergrid.Client.prototype.buildAssetURL = function(uuid) {
* @return {callback} callback(err, data)
*/
Usergrid.Client.prototype.createGroup = function(options, callback) {
- var getOnExist = options.getOnExist || false;
-
- options = {
- path: options.path,
- client: this,
- data: options
- };
-
- var group = new Usergrid.Group(options);
- group.fetch(function(err, data){
- var okToSave = (err && 'service_resource_not_found' === data.error || 'no_name_specified' === data.error || 'null_pointer' === data.error) || (!err && getOnExist);
- if (okToSave) {
- group.save(function(err, data){
- if (typeof(callback) === 'function') {
- callback(err, group);
- }
- });
- } else {
- if(typeof(callback) === 'function') {
- callback(err, group);
- }
- }
- });
+ var getOnExist = options.getOnExist || false;
+ options = {
+ path: options.path,
+ client: this,
+ data: options
+ };
+ var group = new Usergrid.Group(options);
+ group.fetch(function(err, data) {
+ var okToSave = err && "service_resource_not_found" === data.error || "no_name_specified" === data.error || "null_pointer" === data.error || !err && getOnExist;
+ if (okToSave) {
+ group.save(function(err, data) {
+ if (typeof callback === "function") {
+ callback(err, group);
+ }
+ });
+ } else {
+ if (typeof callback === "function") {
+ callback(err, group);
+ }
+ }
+ });
};
/*
@@ -276,10 +310,10 @@ Usergrid.Client.prototype.createGroup = function(options, callback) {
* @param {function} callback
* @return {callback} callback(err, data)
*/
-Usergrid.Client.prototype.createEntity = function (options, callback) {
- // todo: replace the check for new / save on not found code with simple save
- // when users PUT on no user fix is in place.
- /*
+Usergrid.Client.prototype.createEntity = function(options, callback) {
+ // todo: replace the check for new / save on not found code with simple save
+ // when users PUT on no user fix is in place.
+ /*
var options = {
client:this,
data:options
@@ -291,29 +325,30 @@ Usergrid.Client.prototype.createEntity = function (options, callback) {
}
});
*/
- var getOnExist = options.getOnExist || false; //if true, will return entity if one already exists
- var options = {
- client:this,
- data:options
- };
- var entity = new Usergrid.Entity(options);
- entity.fetch(function(err, data) {
- //if the fetch doesn't find what we are looking for, or there is no error, do a save
- var okToSave = (err && 'service_resource_not_found' === data.error || 'no_name_specified' === data.error || 'null_pointer' === data.error) || (!err && getOnExist);
- if(okToSave) {
- entity.set(options.data); //add the data again just in case
- entity.save(function(err, data) {
- if (typeof(callback) === 'function') {
- callback(err, entity, data);
- }
- });
- } else {
- if (typeof(callback) === 'function') {
- callback(err, entity, data);
- }
- }
- });
-
+ var getOnExist = options.getOnExist || false;
+ //if true, will return entity if one already exists
+ var options = {
+ client: this,
+ data: options
+ };
+ var entity = new Usergrid.Entity(options);
+ entity.fetch(function(err, data) {
+ //if the fetch doesn't find what we are looking for, or there is no error, do a save
+ var okToSave = err && "service_resource_not_found" === data.error || "no_name_specified" === data.error || "null_pointer" === data.error || !err && getOnExist;
+ if (okToSave) {
+ entity.set(options.data);
+ //add the data again just in case
+ entity.save(function(err, data) {
+ if (typeof callback === "function") {
+ callback(err, entity, data);
+ }
+ });
+ } else {
+ if (typeof callback === "function") {
+ callback(err, entity, data);
+ }
+ }
+ });
};
/*
@@ -330,17 +365,17 @@ Usergrid.Client.prototype.createEntity = function (options, callback) {
* @param {function} callback
* @return {callback} callback(err, data)
*/
-Usergrid.Client.prototype.getEntity = function (options, callback) {
- var options = {
- client:this,
- data:options
- }
- var entity = new Usergrid.Entity(options);
- entity.fetch(function(err, data) {
- if (typeof(callback) === 'function') {
- callback(err, entity, data);
- }
- });
+Usergrid.Client.prototype.getEntity = function(options, callback) {
+ var options = {
+ client: this,
+ data: options
+ };
+ var entity = new Usergrid.Entity(options);
+ entity.fetch(function(err, data) {
+ if (typeof callback === "function") {
+ callback(err, entity, data);
+ }
+ });
};
/*
@@ -353,14 +388,14 @@ Usergrid.Client.prototype.getEntity = function (options, callback) {
* @param {string} serializedObject
* @return {object} Entity Object
*/
-Usergrid.Client.prototype.restoreEntity = function (serializedObject) {
- var data = JSON.parse(serializedObject);
- var options = {
- client:this,
- data:data
- }
- var entity = new Usergrid.Entity(options);
- return entity;
+Usergrid.Client.prototype.restoreEntity = function(serializedObject) {
+ var data = JSON.parse(serializedObject);
+ var options = {
+ client: this,
+ data: data
+ };
+ var entity = new Usergrid.Entity(options);
+ return entity;
};
/*
@@ -374,13 +409,13 @@ Usergrid.Client.prototype.restoreEntity = function (serializedObject) {
* @param {function} callback
* @return {callback} callback(err, data)
*/
-Usergrid.Client.prototype.createCollection = function (options, callback) {
- options.client = this;
- var collection = new Usergrid.Collection(options, function(err, data) {
- if (typeof(callback) === 'function') {
- callback(err, collection, data);
- }
- });
+Usergrid.Client.prototype.createCollection = function(options, callback) {
+ options.client = this;
+ var collection = new Usergrid.Collection(options, function(err, data) {
+ if (typeof callback === "function") {
+ callback(err, collection, data);
+ }
+ });
};
/*
@@ -393,11 +428,11 @@ Usergrid.Client.prototype.createCollection = function (options, callback) {
* @param {string} serializedObject
* @return {object} Collection Object
*/
-Usergrid.Client.prototype.restoreCollection = function (serializedObject) {
- var data = JSON.parse(serializedObject);
- data.client = this;
- var collection = new Usergrid.Collection(data);
- return collection;
+Usergrid.Client.prototype.restoreCollection = function(serializedObject) {
+ var data = JSON.parse(serializedObject);
+ data.client = this;
+ var collection = new Usergrid.Collection(data);
+ return collection;
};
/*
@@ -410,20 +445,19 @@ Usergrid.Client.prototype.restoreCollection = function (serializedObject) {
* @return {callback} callback(err, data, activities)
*/
Usergrid.Client.prototype.getFeedForUser = function(username, callback) {
- var options = {
- method: "GET",
- endpoint: "users/"+username+"/feed"
- };
-
- this.request(options, function(err, data){
- if(typeof(callback) === "function") {
- if(err) {
- callback(err);
- } else {
- callback(err, data, data.entities);
- }
- }
- });
+ var options = {
+ method: "GET",
+ endpoint: "users/" + username + "/feed"
+ };
+ this.request(options, function(err, data) {
+ if (typeof callback === "function") {
+ if (err) {
+ callback(err);
+ } else {
+ callback(err, data, data.entities);
+ }
+ }
+ });
};
/*
@@ -461,18 +495,18 @@ Usergrid.Client.prototype.getFeedForUser = function(username, callback) {
* @param {function} callback
* @return {callback} callback(err, data)
*/
-Usergrid.Client.prototype.createUserActivity = function (user, options, callback) {
- options.type = 'users/'+user+'/activities';
- var options = {
- client:this,
- data:options
- }
- var entity = new Usergrid.Entity(options);
- entity.save(function(err, data) {
- if (typeof(callback) === 'function') {
- callback(err, entity);
- }
- });
+Usergrid.Client.prototype.createUserActivity = function(user, options, callback) {
+ options.type = "users/" + user + "/activities";
+ var options = {
+ client: this,
+ data: options
+ };
+ var entity = new Usergrid.Entity(options);
+ entity.save(function(err, data) {
+ if (typeof callback === "function") {
+ callback(err, entity);
+ }
+ });
};
/*
@@ -489,41 +523,39 @@ Usergrid.Client.prototype.createUserActivity = function (user, options, callback
* @return {callback} callback(err, data)
*/
Usergrid.Client.prototype.createUserActivityWithEntity = function(user, content, callback) {
- var username = user.get("username");
- var options = {
- actor: {
- "displayName":username,
- "uuid":user.get("uuid"),
- "username":username,
- "email":user.get("email"),
- "picture":user.get("picture"),
- "image": {
- "duration":0,
- "height":80,
- "url":user.get("picture"),
- "width":80
- },
- },
- "verb":"post",
- "content":content
- };
-
- this.createUserActivity(username, options, callback);
-
+ var username = user.get("username");
+ var options = {
+ actor: {
+ displayName: username,
+ uuid: user.get("uuid"),
+ username: username,
+ email: user.get("email"),
+ picture: user.get("picture"),
+ image: {
+ duration: 0,
+ height: 80,
+ url: user.get("picture"),
+ width: 80
+ }
+ },
+ verb: "post",
+ content: content
+ };
+ this.createUserActivity(username, options, callback);
};
/*
* A private method to get call timing of last call
*/
-Usergrid.Client.prototype.calcTimeDiff = function () {
- var seconds = 0;
- var time = this._end - this._start;
- try {
- seconds = ((time/10) / 60).toFixed(2);
- } catch(e) {
- return 0;
- }
- return seconds;
+Usergrid.Client.prototype.calcTimeDiff = function() {
+ var seconds = 0;
+ var time = this._end - this._start;
+ try {
+ seconds = (time / 10 / 60).toFixed(2);
+ } catch (e) {
+ return 0;
+ }
+ return seconds;
};
/*
@@ -534,8 +566,8 @@ Usergrid.Client.prototype.calcTimeDiff = function () {
* @params {string} token
* @return none
*/
-Usergrid.Client.prototype.setToken = function (token) {
- this.set('token', token);
+Usergrid.Client.prototype.setToken = function(token) {
+ this.set("token", token);
};
/*
@@ -545,41 +577,41 @@ Usergrid.Client.prototype.setToken = function (token) {
* @public
* @return {string} token
*/
-Usergrid.Client.prototype.getToken = function () {
- return this.get('token');
+Usergrid.Client.prototype.getToken = function() {
+ return this.get("token");
};
Usergrid.Client.prototype.setObject = function(key, value) {
- if (value) {
- value = JSON.stringify(value);
- }
- this.set(key, value);
+ if (value) {
+ value = JSON.stringify(value);
+ }
+ this.set(key, value);
};
-Usergrid.Client.prototype.set = function (key, value) {
- var keyStore = 'apigee_' + key;
- this[key] = value;
- if(typeof(Storage)!=="undefined"){
- if (value) {
- localStorage.setItem(keyStore, value);
- } else {
- localStorage.removeItem(keyStore);
+Usergrid.Client.prototype.set = function(key, value) {
+ var keyStore = "apigee_" + key;
+ this[key] = value;
+ if (typeof Storage !== "undefined") {
+ if (value) {
+ localStorage.setItem(keyStore, value);
+ } else {
+ localStorage.removeItem(keyStore);
+ }
}
- }
};
Usergrid.Client.prototype.getObject = function(key) {
- return JSON.parse(this.get(key));
+ return JSON.parse(this.get(key));
};
-Usergrid.Client.prototype.get = function (key) {
- var keyStore = 'apigee_' + key;
- if (this[key]) {
- return this[key];
- } else if(typeof(Storage)!=="undefined") {
- return localStorage.getItem(keyStore);
- }
- return null;
+Usergrid.Client.prototype.get = function(key) {
+ var keyStore = "apigee_" + key;
+ if (this[key]) {
+ return this[key];
+ } else if (typeof Storage !== "undefined") {
+ return localStorage.getItem(keyStore);
+ }
+ return null;
};
/*
@@ -595,16 +627,15 @@ Usergrid.Client.prototype.get = function (key) {
* @return {callback} callback(err, data)
*/
Usergrid.Client.prototype.signup = function(username, password, email, name, callback) {
- var self = this;
- var options = {
- type:"users",
- username:username,
- password:password,
- email:email,
- name:name
- };
-
- this.createEntity(options, callback);
+ var self = this;
+ var options = {
+ type: "users",
+ username: username,
+ password: password,
+ email: email,
+ name: name
+ };
+ this.createEntity(options, callback);
};
/*
@@ -618,122 +649,112 @@ Usergrid.Client.prototype.signup = function(username, password, email, name, cal
* @param {function} callback
* @return {callback} callback(err, data)
*/
-Usergrid.Client.prototype.login = function (username, password, callback) {
- var self = this;
- var options = {
- method:'POST',
- endpoint:'token',
- body:{
- username: username,
- password: password,
- grant_type: 'password'
- }
- };
- this.request(options, function(err, data) {
- var user = {};
- if (err && self.logging) {
- console.log('error trying to log user in');
- } else {
- var options = {
- client:self,
- data:data.user
- };
- user = new Usergrid.Entity(options);
- self.setToken(data.access_token);
- }
- if (typeof(callback) === 'function') {
- callback(err, data, user);
- }
- });
+Usergrid.Client.prototype.login = function(username, password, callback) {
+ var self = this;
+ var options = {
+ method: "POST",
+ endpoint: "token",
+ body: {
+ username: username,
+ password: password,
+ grant_type: "password"
+ }
+ };
+ this.request(options, function(err, data) {
+ var user = {};
+ if (err && self.logging) {
+ console.log("error trying to log user in");
+ } else {
+ var options = {
+ client: self,
+ data: data.user
+ };
+ user = new Usergrid.Entity(options);
+ self.setToken(data.access_token);
+ }
+ if (typeof callback === "function") {
+ callback(err, data, user);
+ }
+ });
};
-
-Usergrid.Client.prototype.reAuthenticateLite = function (callback) {
- var self = this;
- var options = {
- method:'GET',
- endpoint:'management/me',
- mQuery:true
- };
- this.request(options, function(err, response) {
- if (err && self.logging) {
- console.log('error trying to re-authenticate user');
- } else {
-
- //save the re-authed token and current email/username
- self.setToken(response.access_token);
-
- }
- if (typeof(callback) === 'function') {
- callback(err);
- }
- });
-};
-
-
-Usergrid.Client.prototype.reAuthenticate = function (email, callback) {
- var self = this;
- var options = {
- method:'GET',
- endpoint:'management/users/'+email,
- mQuery:true
- };
- this.request(options, function(err, response) {
- var organizations = {};
- var applications = {};
- var user = {};
- var data;
- if (err && self.logging) {
- console.log('error trying to full authenticate user');
- } else {
- data = response.data;
- self.setToken(data.token);
- self.set('email', data.email);
-
- //delete next block and corresponding function when iframes are refactored
- localStorage.setItem('accessToken', data.token);
- localStorage.setItem('userUUID', data.uuid);
- localStorage.setItem('userEmail', data.email);
- //end delete block
-
-
- var userData = {
- "username" : data.username,
- "email" : data.email,
- "name" : data.name,
- "uuid" : data.uuid
- };
- var options = {
- client:self,
- data:userData
- };
- user = new Usergrid.Entity(options);
-
- organizations = data.organizations;
- var org = '';
- try {
- //if we have an org stored, then use that one. Otherwise, use the first one.
- var existingOrg = self.get('orgName');
- org = (organizations[existingOrg])?organizations[existingOrg]:organizations[Object.keys(organizations)[0]];
- self.set('orgName', org.name);
- } catch(e) {
- err = true;
- if (self.logging) {
- console.log('error selecting org');
+Usergrid.Client.prototype.reAuthenticateLite = function(callback) {
+ var self = this;
+ var options = {
+ method: "GET",
+ endpoint: "management/me",
+ mQuery: true
+ };
+ this.request(options, function(err, response) {
+ if (err && self.logging) {
+ console.log("error trying to re-authenticate user");
+ } else {
+ //save the re-authed token and current email/username
+ self.setToken(response.access_token);
}
- } //should always be an org
-
- applications = self.parseApplicationsArray(org);
- self.selectFirstApp(applications);
-
- self.setObject('organizations', organizations);
- self.setObject('applications', applications);
+ if (typeof callback === "function") {
+ callback(err);
+ }
+ });
+};
- }
- if (typeof(callback) === 'function') {
- callback(err, data, user, organizations, applications);
- }
- });
+Usergrid.Client.prototype.reAuthenticate = function(email, callback) {
+ var self = this;
+ var options = {
+ method: "GET",
+ endpoint: "management/users/" + email,
+ mQuery: true
+ };
+ this.request(options, function(err, response) {
+ var organizations = {};
+ var applications = {};
+ var user = {};
+ var data;
+ if (err && self.logging) {
+ console.log("error trying to full authenticate user");
+ } else {
+ data = response.data;
+ self.setToken(data.token);
+ self.set("email", data.email);
+ //delete next block and corresponding function when iframes are refactored
+ localStorage.setItem("accessToken", data.token);
+ localStorage.setItem("userUUID", data.uuid);
+ localStorage.setItem("userEmail", data.email);
+ //end delete block
+ var userData = {
+ username: data.username,
+ email: data.email,
+ name: data.name,
+ uuid: data.uuid
+ };
+ var options = {
+ client: self,
+ data: userData
+ };
+ user = new Usergrid.Entity(options);
+ organizations = data.organizations;
+ var org = "";
+ try {
+ //if we have an org stored, then use that one. Otherwise, use the first one.
+ var existingOrg = self.get("orgName");
+ org = organizations[existingOrg] ? organizations[existingOrg] : organizations[Object.keys(organizations)[0]];
+ self.set("orgName", org.name);
+ } catch (e) {
+ err = true;
+ if (self.logging) {
+ console.log("error selecting org");
+ }
+ }
+ //should always be an org
+ applications = self.parseApplicationsArray(org);
+ self.selectFirstApp(applications);
+ self.setObject("organizations", organizations);
+ self.setObject("applications", applications);
+ }
+ if (typeof callback === "function") {
+ callback(err, data, user, organizations, applications);
+ }
+ });
};
/*
@@ -746,31 +767,31 @@ Usergrid.Client.prototype.reAuthenticate = function (email, callback) {
* @param {function} callback
* @return {callback} callback(err, data)
*/
-Usergrid.Client.prototype.loginFacebook = function (facebookToken, callback) {
- var self = this;
- var options = {
- method:'GET',
- endpoint:'auth/facebook',
- qs:{
- fb_access_token: facebookToken
- }
- };
- this.request(options, function(err, data) {
- var user = {};
- if (err && self.logging) {
- console.log('error trying to log user in');
- } else {
- var options = {
- client: self,
- data: data.user
- }
- user = new Usergrid.Entity(options);
- self.setToken(data.access_token);
- }
- if (typeof(callback) === 'function') {
- callback(err, data, user);
- }
- });
+Usergrid.Client.prototype.loginFacebook = function(facebookToken, callback) {
+ var self = this;
+ var options = {
+ method: "GET",
+ endpoint: "auth/facebook",
+ qs: {
+ fb_access_token: facebookToken
+ }
+ };
+ this.request(options, function(err, data) {
+ var user = {};
+ if (err && self.logging) {
+ console.log("error trying to log user in");
+ } else {
+ var options = {
+ client: self,
+ data: data.user
+ };
+ user = new Usergrid.Entity(options);
+ self.setToken(data.access_token);
+ }
+ if (typeof callback === "function") {
+ callback(err, data, user);
+ }
+ });
};
/*
@@ -781,35 +802,35 @@ Usergrid.Client.prototype.loginFacebook = function (facebookToken, callback) {
* @param {function} callback
* @return {callback} callback(err, data)
*/
-Usergrid.Client.prototype.getLoggedInUser = function (callback) {
- if (!this.getToken()) {
- callback(true, null, null);
- } else {
- var self = this;
- var options = {
- method:'GET',
- endpoint:'users/me'
- };
- this.request(options, function(err, data) {
- if (err) {
- if (self.logging) {
- console.log('error trying to log user in');
- }
- if (typeof(callback) === 'function') {
- callback(err, data, null);
- }
- } else {
+Usergrid.Client.prototype.getLoggedInUser = function(callback) {
+ if (!this.getToken()) {
+ callback(true, null, null);
+ } else {
+ var self = this;
var options = {
- client:self,
- data:data.entities[0]
+ method: "GET",
+ endpoint: "users/me"
};
- var user = new Usergrid.Entity(options);
- if (typeof(callback) === 'function') {
- callback(err, data, user);
- }
- }
- });
- }
+ this.request(options, function(err, data) {
+ if (err) {
+ if (self.logging) {
+ console.log("error trying to log user in");
+ }
+ if (typeof callback === "function") {
+ callback(err, data, null);
+ }
+ } else {
+ var options = {
+ client: self,
+ data: data.entities[0]
+ };
+ var user = new Usergrid.Entity(options);
+ if (typeof callback === "function") {
+ callback(err, data, user);
+ }
+ }
+ });
+ }
};
/*
@@ -820,11 +841,11 @@ Usergrid.Client.prototype.getLoggedInUser = function (callback) {
* @public
* @return {boolean} Returns true the user is logged in (has token and uuid), false if not
*/
-Usergrid.Client.prototype.isLoggedIn = function () {
- if (this.getToken() && this.getToken() != 'null') {
- return true;
- }
- return false;
+Usergrid.Client.prototype.isLoggedIn = function() {
+ if (this.getToken() && this.getToken() != "null") {
+ return true;
+ }
+ return false;
};
/*
@@ -834,8 +855,8 @@ Usergrid.Client.prototype.isLoggedIn = function () {
* @public
* @return none
*/
-Usergrid.Client.prototype.logout = function () {
- this.setToken(null);
+Usergrid.Client.prototype.logout = function() {
+ this.setToken(null);
};
/*
@@ -846,54 +867,52 @@ Usergrid.Client.prototype.logout = function () {
* @param {object} options
* @return {string} curl
*/
-Usergrid.Client.prototype.buildCurlCall = function (options) {
- var curl = 'curl';
- var method = (options.method || 'GET').toUpperCase();
- var body = options.body || {};
- var uri = options.uri;
-
- //curl - add the method to the command (no need to add anything for GET)
- if (method === 'POST') {
- curl += ' -X POST';
- } else if (method === 'PUT') {
- curl += ' -X PUT';
- } else if (method === 'DELETE') {
- curl += ' -X DELETE';
- } else {
- curl += ' -X GET';
- }
-
- //curl - append the path
- curl += ' ' + uri;
-
- //curl - add the body
- if("undefined"!== typeof window){body = JSON.stringify(body);}//only in node module
- if (body !== '"{}"' && method !== 'GET' && method !== 'DELETE') {
- //curl - add in the json obj
- curl += " -d '" + body + "'";
- }
-
- //log the curl command to the console
- console.log(curl);
-
- return curl;
-}
-
-Usergrid.Client.prototype.getDisplayImage = function (email, picture, size) {
- try {
- if (picture) {
- return picture;
- }
- var size = size || 50;
- if (email.length) {
- return 'https://secure.gravatar.com/avatar/' + MD5(email) + '?s=' + size + encodeURI("&d=https://apigee.com/usergrid/images/user_profile.png");
+Usergrid.Client.prototype.buildCurlCall = function(options) {
+ var curl = "curl";
+ var method = (options.method || "GET").toUpperCase();
+ var body = options.body || {};
+ var uri = options.uri;
+ //curl - add the method to the command (no need to add anything for GET)
+ if (method === "POST") {
+ curl += " -X POST";
+ } else if (method === "PUT") {
+ curl += " -X PUT";
+ } else if (method === "DELETE") {
+ curl += " -X DELETE";
} else {
- return 'https://apigee.com/usergrid/images/user_profile.png';
+ curl += " -X GET";
}
- } catch(e) {
- return 'https://apigee.com/usergrid/images/user_profile.png';
- }
-}
+ //curl - append the path
+ curl += " " + uri;
+ //curl - add the body
+ if ("undefined" !== typeof window) {
+ body = JSON.stringify(body);
+ }
+ //only in node module
+ if (body !== '"{}"' && method !== "GET" && method !== "DELETE") {
+ //curl - add in the json obj
+ curl += " -d '" + body + "'";
+ }
+ //log the curl command to the console
+ console.log(curl);
+ return curl;
+};
+
+Usergrid.Client.prototype.getDisplayImage = function(email, picture, size) {
+ try {
+ if (picture) {
+ return picture;
+ }
+ var size = size || 50;
+ if (email.length) {
+ return "https://secure.gravatar.com/avatar/" + MD5(email) + "?s=" + size + encodeURI("&d=https://apigee.com/usergrid/images/user_profile.png");
+ } else {
+ return "https://apigee.com/usergrid/images/user_profile.png";
+ }
+ } catch (e) {
+ return "https://apigee.com/usergrid/images/user_profile.png";
+ }
+};
/*
* A class to Model a Usergrid Entity.
@@ -903,10 +922,10 @@ Usergrid.Client.prototype.getDisplayImage = function (email, picture, size) {
* @param {object} options {client:client, data:{'type':'collection_type', uuid:'uuid', 'key':'value'}}
*/
Usergrid.Entity = function(options) {
- if (options) {
- this._data = options.data || {};
- this._client = options.client || {};
- }
+ if (options) {
+ this._data = options.data || {};
+ this._client = options.client || {};
+ }
};
/*
@@ -917,8 +936,8 @@ Usergrid.Entity = function(options) {
* @method serialize
* @return {string} data
*/
-Usergrid.Entity.prototype.serialize = function () {
- return JSON.stringify(this._data);
+Usergrid.Entity.prototype.serialize = function() {
+ return JSON.stringify(this._data);
};
/*
@@ -929,12 +948,12 @@ Usergrid.Entity.prototype.serialize = function () {
* @param {string} field
* @return {string} || {object} data
*/
-Usergrid.Entity.prototype.get = function (field) {
- if (field) {
- return this._data[field];
- } else {
- return this._data;
- }
+Usergrid.Entity.prototype.get = function(field) {
+ if (field) {
+ return this._data[field];
+ } else {
+ return this._data;
+ }
};
/*
@@ -947,20 +966,20 @@ Usergrid.Entity.prototype.get = function (field) {
* @param {string} value
* @return none
*/
-Usergrid.Entity.prototype.set = function (key, value) {
- if (typeof key === 'object') {
- for(var field in key) {
- this._data[field] = key[field];
- }
- } else if (typeof key === 'string') {
- if (value === null) {
- delete this._data[key];
+Usergrid.Entity.prototype.set = function(key, value) {
+ if (typeof key === "object") {
+ for (var field in key) {
+ this._data[field] = key[field];
+ }
+ } else if (typeof key === "string") {
+ if (value === null) {
+ delete this._data[key];
+ } else {
+ this._data[key] = value;
+ }
} else {
- this._data[key] = value;
+ this._data = {};
}
- } else {
- this._data = {};
- }
};
/*
@@ -971,88 +990,86 @@ Usergrid.Entity.prototype.set = function (key, value) {
* @param {function} callback
* @return {callback} callback(err, data)
*/
-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();
- var password = this.get('password');
- var oldpassword = this.get('oldpassword');
- var newpassword = this.get('newpassword');
- //remove system specific properties
- for (var item in entityData) {
- if (item === 'metadata' || item === 'created' || item === 'modified' ||
- item === 'oldpassword' || item === 'newpassword' || //old and new pw not added to data
- 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) {
- //clear out pw info if present
- self.set('password', null);
- self.set('oldpassword', null);
- self.set('newpassword', null);
- 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);
- var path = retdata.path;
- //for connections, API returns type
- while (path.substring(0, 1) === "/") {
- path = path.substring(1);
- }
- self.set('type', path);
- }
- }
- //if this is a user, update the password if it has been specified;
- var needPasswordChange = ((self.get('type') === 'user' || self.get('type') === 'users') && oldpassword && 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 = oldpassword;
- pwdata.newpassword = 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);
- }
+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();
+ var password = this.get("password");
+ var oldpassword = this.get("oldpassword");
+ var newpassword = this.get("newpassword");
+ //remove system specific properties
+ for (var item in entityData) {
+ if (item === "metadata" || item === "created" || item === "modified" || item === "oldpassword" || item === "newpassword" || //old and new pw not added to data
+ 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) {
+ //clear out pw info if present
+ self.set("password", null);
+ self.set("oldpassword", null);
+ self.set("newpassword", null);
+ 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);
+ var path = retdata.path;
+ //for connections, API returns type
+ while (path.substring(0, 1) === "/") {
+ path = path.substring(1);
+ }
+ self.set("type", path);
+ }
+ }
+ //if this is a user, update the password if it has been specified;
+ var needPasswordChange = (self.get("type") === "user" || self.get("type") === "users") && oldpassword && 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 = oldpassword;
+ pwdata.newpassword = 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);
+ }
+ }
+ });
};
/*
@@ -1063,93 +1080,92 @@ Usergrid.Entity.prototype.save = function (callback) {
* @param {function} callback
* @return {callback} callback(err, data)
*/
-Usergrid.Entity.prototype.fetch = function (callback) {
- var type = this.get('type');
- var self = this;
-
- //Check for an entity type, then if a uuid is available, use that, otherwise, use the name
- try {
- if (type === undefined) {
- throw 'cannot fetch entity, no entity type specified'
- } else if (this.get('uuid')) {
- type += '/' + this.get('uuid');
- } else if (type === 'users' && this.get('username')) {
- type += '/' + this.get('username');
- } else if (this.get('name')) {
- type += '/' + encodeURIComponent(this.get('name'));
- } else if (typeof(callback) === 'function') {
- throw 'no_name_specified';
- }
- } catch (e) {
- if (self._client.logging) {
- console.log(e);
- }
- return callback(true, {
- error: e
- }, self);
- }
- var options = {
- method:'GET',
- endpoint:type
- };
- this._client.request(options, function (err, data) {
- if (err && self._client.logging) {
- console.log('could not get entity');
- } else {
- if (data.user) {
- self.set(data.user);
- self._json = JSON.stringify(data.user, null, 2);
- } else if (data.entities) {
- if (data.entities.length) {
- var entity = data.entities[0];
- self.set(entity);
- }
- }
- }
- if (typeof(callback) === 'function') {
- callback(err, data, self);
+Usergrid.Entity.prototype.fetch = function(callback) {
+ var type = this.get("type");
+ var self = this;
+ //Check for an entity type, then if a uuid is available, use that, otherwise, use the name
+ try {
+ if (type === undefined) {
+ throw "cannot fetch entity, no entity type specified";
+ } else if (this.get("uuid")) {
+ type += "/" + this.get("uuid");
+ } else if (type === "users" && this.get("username")) {
+ type += "/" + this.get("username");
+ } else if (this.get("name")) {
+ type += "/" + encodeURIComponent(this.get("name"));
+ } else if (typeof callback === "function") {
+ throw "no_name_specified";
+ }
+ } catch (e) {
+ if (self._client.logging) {
+ console.log(e);
+ }
+ return callback(true, {
+ error: e
+ }, self);
}
- });
-};
-
-/*
- * deletes the entity from the database - will only delete
- * if the object has a valid uuid
- *
- * @method destroy
+ var options = {
+ method: "GET",
+ endpoint: type
+ };
+ this._client.request(options, function(err, data) {
+ if (err && self._client.logging) {
+ console.log("could not get entity");
+ } else {
+ if (data.user) {
+ self.set(data.user);
+ self._json = JSON.stringify(data.user, null, 2);
+ } else if (data.entities) {
+ if (data.entities.length) {
+ var entity = data.entities[0];
+ self.set(entity);
+ }
+ }
+ }
+ if (typeof callback === "function") {
+ callback(err, data, self);
+ }
+ });
+};
+
+/*
+ * deletes the entity from the database - will only delete
+ * if the object has a valid uuid
+ *
+ * @method destroy
* @public
* @param {function} callback
* @return {callback} callback(err, data)
*
*/
-Usergrid.Entity.prototype.destroy = function (callback) {
- var self = this;
- var type = this.get('type');
- if (isUUID(this.get('uuid'))) {
- type += '/' + this.get('uuid');
- } else {
- if (typeof(callback) === 'function') {
- var error = 'Error trying to delete object - no uuid specified.';
- if (self._client.logging) {
- console.log(error);
- }
- callback(true, error);
- }
- }
- var options = {
- method:'DELETE',
- endpoint:type
- };
- this._client.request(options, function (err, data) {
- if (err && self._client.logging) {
- console.log('entity could not be deleted');
+Usergrid.Entity.prototype.destroy = function(callback) {
+ var self = this;
+ var type = this.get("type");
+ if (isUUID(this.get("uuid"))) {
+ type += "/" + this.get("uuid");
} else {
- self.set(null);
- }
- if (typeof(callback) === 'function') {
- callback(err, data);
+ if (typeof callback === "function") {
+ var error = "Error trying to delete object - no uuid specified.";
+ if (self._client.logging) {
+ console.log(error);
+ }
+ callback(true, error);
+ }
}
- });
+ var options = {
+ method: "DELETE",
+ endpoint: type
+ };
+ this._client.request(options, function(err, data) {
+ if (err && self._client.logging) {
+ console.log("entity could not be deleted");
+ } else {
+ self.set(null);
+ }
+ if (typeof callback === "function") {
+ callback(err, data);
+ }
+ });
};
/*
@@ -1163,52 +1179,48 @@ Usergrid.Entity.prototype.destroy = function (callback) {
* @return {callback} callback(err, data)
*
*/
-Usergrid.Entity.prototype.connect = function (connection, entity, callback) {
-
- var self = this;
-
- var error;
- //connectee info
- var connecteeType = entity.get('type');
- var connectee = this.getEntityId(entity);
- if (!connectee) {
- if (typeof(callback) === 'function') {
- error = 'Error trying to delete object - no uuid specified.';
- if (self._client.logging) {
- console.log(error);
- }
- callback(true, error);
- }
- return;
- }
-
- //connector info
- var connectorType = this.get('type');
- var connector = this.getEntityId(this);
- if (!connector) {
- if (typeof(callback) === 'function') {
- error = 'Error in connect - no uuid specified.';
- if (self._client.logging) {
- console.log(error);
- }
- callback(true, error);
- }
- return;
- }
-
- var endpoint = connectorType + '/' + connector + '/' + connection + '/' + connecteeType + '/' + connectee;
- var options = {
- method:'POST',
- endpoint:endpoint
- };
- this._client.request(options, function (err, data) {
- if (err && self._client.logging) {
- console.log('entity could not be connected');
- }
- if (typeof(callback) === 'function') {
- callback(err, data);
+Usergrid.Entity.prototype.connect = function(connection, entity, callback) {
+ var self = this;
+ var error;
+ //connectee info
+ var connecteeType = entity.get("type");
+ var connectee = this.getEntityId(entity);
+ if (!connectee) {
+ if (typeof callback === "function") {
+ error = "Error trying to delete object - no uuid specified.";
+ if (self._client.logging) {
+ console.log(error);
+ }
+ callback(true, error);
+ }
+ return;
+ }
+ //connector info
+ var connectorType = this.get("type");
+ var connector = this.getEntityId(this);
+ if (!connector) {
+ if (typeof callback === "function") {
+ error = "Error in connect - no uuid specified.";
+ if (self._client.logging) {
+ console.log(error);
+ }
+ callback(true, error);
+ }
+ return;
}
- });
+ var endpoint = connectorType + "/" + connector + "/" + connection + "/" + connecteeType + "/" + connectee;
+ var options = {
+ method: "POST",
+ endpoint: endpoint
+ };
+ this._client.request(options, function(err, data) {
+ if (err && self._client.logging) {
+ console.log("entity could not be connected");
+ }
+ if (typeof callback === "function") {
+ callback(err, data);
+ }
+ });
};
/*
@@ -1221,18 +1233,18 @@ Usergrid.Entity.prototype.connect = function (connection, entity, callback) {
* @return {callback} callback(err, data)
*
*/
-Usergrid.Entity.prototype.getEntityId = function (entity) {
- var id = false;
- if (isUUID(entity.get('uuid'))) {
- id = entity.get('uuid');
- } else {
- if (type === 'users') {
- id = entity.get('username');
- } else if (entity.get('name')) {
- id = entity.get('name');
+Usergrid.Entity.prototype.getEntityId = function(entity) {
+ var id = false;
+ if (isUUID(entity.get("uuid"))) {
+ id = entity.get("uuid");
+ } else {
+ if (type === "users") {
+ id = entity.get("username");
+ } else if (entity.get("name")) {
+ id = entity.get("name");
+ }
}
- }
- return id;
+ return id;
};
/*
@@ -1246,241 +1258,195 @@ Usergrid.Entity.prototype.getEntityId = function (entity) {
* @return {callback} callback(err, data, connections)
*
*/
-Usergrid.Entity.prototype.getConnections = function (connection, callback) {
-
- var self = this;
-
- //connector info
- var connectorType = this.get('type');
- var connector = this.getEntityId(this);
- if (!connector) {
- if (typeof(callback) === 'function') {
- var error = 'Error in getConnections - no uuid specified.';
- if (self._client.logging) {
- console.log(error);
- }
- callback(true, error);
- }
- return;
- }
-
- var endpoint = connectorType + '/' + connector + '/' + connection + '/';
- var options = {
- method:'GET',
- endpoint:endpoint
- };
- this._client.request(options, function (err, data) {
- if (err && self._client.logging) {
- console.log('entity could not be connected');
- }
-
- self[connection] = {};
-
- var length = data.entities.length;
- for (var i = 0; i < length; i++) {
- if (data.entities[i].type === 'user'){
- self[connection][data.entities[i].username] = data.entities[i];
- } else {
- self[connection][data.entities[i].name] = data.entities[i]
- }
- }
-
- if (typeof(callback) === 'function') {
- callback(err, data, data.entities);
+Usergrid.Entity.prototype.getConnections = function(connection, callback) {
+ var self = this;
+ //connector info
+ var connectorType = this.get("type");
+ var connector = this.getEntityId(this);
+ if (!connector) {
+ if (typeof callback === "function") {
+ var error = "Error in getConnections - no uuid specified.";
+ if (self._client.logging) {
+ console.log(error);
+ }
+ callback(true, error);
+ }
+ return;
}
- });
-
+ var endpoint = connectorType + "/" + connector + "/" + connection + "/";
+ var options = {
+ method: "GET",
+ endpoint: endpoint
+ };
+ this._client.request(options, function(err, data) {
+ if (err && self._client.logging) {
+ console.log("entity could not be connected");
+ }
+ self[connection] = {};
+ var length = data.entities.length;
+ for (var i = 0; i < length; i++) {
+ if (data.entities[i].type === "user") {
+ self[connection][data.entities[i].username] = data.entities[i];
+ } else {
+ self[connection][data.entities[i].name] = data.entities[i];
+ }
+ }
+ if (typeof callback === "function") {
+ callback(err, data, data.entities);
+ }
+ });
};
-Usergrid.Entity.prototype.getGroups = function (callback) {
-
- var self = this;
-
- var endpoint = 'users' + '/' + this.get('uuid') + '/groups' ;
- var options = {
- method:'GET',
- endpoint:endpoint
- };
- this._client.request(options, function (err, data) {
- if (err && self._client.logging) {
- console.log('entity could not be connected');
- }
-
- self.groups = data.entities;
-
- if (typeof(callback) === 'function') {
- callback(err, data, data.entities);
- }
- });
-
+Usergrid.Entity.prototype.getGroups = function(callback) {
+ var self = this;
+ var endpoint = "users" + "/" + this.get("uuid") + "/groups";
+ var options = {
+ method: "GET",
+ endpoint: endpoint
+ };
+ this._client.request(options, function(err, data) {
+ if (err && self._client.logging) {
+ console.log("entity could not be connected");
+ }
+ self.groups = data.entities;
+ if (typeof callback === "function") {
+ callback(err, data, data.entities);
+ }
+ });
};
-Usergrid.Entity.prototype.getActivities = function (callback) {
-
- var self = this;
-
- var endpoint = this.get('type') + '/' + this.get('uuid') + '/activities' ;
- var options = {
- method:'GET',
- endpoint:endpoint
- };
- this._client.request(options, function (err, data) {
- if (err && self._client.logging) {
- console.log('entity could not be connected');
- }
-
- for (var entity in data.entities) {
- data.entities[entity].createdDate = (new Date(data.entities[entity].created)).toUTCString();
- }
-
- self.activities = data.entities;
-
- if (typeof(callback) === 'function') {
- callback(err, data, data.entities);
- }
- });
-
+Usergrid.Entity.prototype.getActivities = function(callback) {
+ var self = this;
+ var endpoint = this.get("type") + "/" + this.get("uuid") + "/activities";
+ var options = {
+ method: "GET",
+ endpoint: endpoint
+ };
+ this._client.request(options, function(err, data) {
+ if (err && self._client.logging) {
+ console.log("entity could not be connected");
+ }
+ for (var entity in data.entities) {
+ data.entities[entity].createdDate = new Date(data.entities[entity].created).toUTCString();
+ }
+ self.activities = data.entities;
+ if (typeof callback === "function") {
+ callback(err, data, data.entities);
+ }
+ });
};
-Usergrid.Entity.prototype.getFollowing = function (callback) {
-
- var self = this;
-
- var endpoint = 'users' + '/' + this.get('uuid') + '/following' ;
- var options = {
- method:'GET',
- endpoint:endpoint
- };
- this._client.request(options, function (err, data) {
- if (err && self._client.logging) {
- console.log('could not get user following');
- }
-
- for (var entity in data.entities) {
- data.entities[entity].createdDate = (new Date(data.entities[entity].created)).toUTCString();
- var image = self._client.getDisplayImage(data.entities[entity].email, data.entities[entity].picture);
- data.entities[entity]._portal_image_icon = image;
- }
-
- self.following = data.entities;
-
- if (typeof(callback) === 'function') {
- callback(err, data, data.entities);
- }
- });
-
+Usergrid.Entity.prototype.getFollowing = function(callback) {
+ var self = this;
+ var endpoint = "users" + "/" + this.get("uuid") + "/following";
+ var options = {
+ method: "GET",
+ endpoint: endpoint
+ };
+ this._client.request(options, function(err, data) {
+ if (err && self._client.logging) {
+ console.log("could not get user following");
+ }
+ for (var entity in data.entities) {
+ data.entities[entity].createdDate = new Date(data.entities[entity].created).toUTCString();
+ var image = self._client.getDisplayImage(data.entities[entity].email, data.entities[entity].picture);
+ data.entities[entity]._portal_image_icon = image;
+ }
+ self.following = data.entities;
+ if (typeof callback === "function") {
+ callback(err, data, data.entities);
+ }
+ });
};
-
-Usergrid.Entity.prototype.getFollowers = function (callback) {
-
- var self = this;
-
- var endpoint = 'users' + '/' + this.get('uuid') + '/followers' ;
- var options = {
- method:'GET',
- endpoint:endpoint
- };
- this._client.request(options, function (err, data) {
- if (err && self._client.logging) {
- console.log('could not get user followers');
- }
-
- for (var entity in data.entities) {
- data.entities[entity].createdDate = (new Date(data.entities[entity].created)).toUTCString();
- var image = self._client.getDisplayImage(data.entities[entity].email, data.entities[entity].picture);
- data.entities[entity]._portal_image_icon = image;
- }
-
- self.followers = data.entities;
-
- if (typeof(callback) === 'function') {
- callback(err, data, data.entities);
- }
- });
-
+Usergrid.Entity.prototype.getFollowers = function(callback) {
+ var self = this;
+ var endpoint = "users" + "/" + this.get("uuid") + "/followers";
+ var options = {
+ method: "GET",
+ endpoint: endpoint
+ };
+ this._client.request(options, function(err, data) {
+ if (err && self._client.logging) {
+ console.log("could not get user followers");
+ }
+ for (var entity in data.entities) {
+ data.entities[entity].createdDate = new Date(data.entities[entity].created).toUTCString();
+ var image = self._client.getDisplayImage(data.entities[entity].email, data.entities[entity].picture);
+ data.entities[entity]._portal_image_icon = image;
+ }
+ self.followers = data.entities;
+ if (typeof callback === "function") {
+ callback(err, data, data.entities);
+ }
+ });
};
-Usergrid.Entity.prototype.getRoles = function (callback) {
-
- var self = this;
-
- var endpoint = this.get('type') + '/' + this.get('uuid') + '/roles' ;
- var options = {
- method:'GET',
- endpoint:endpoint
- };
- this._client.request(options, function (err, data) {
- if (err && self._client.logging) {
- console.log('could not get user roles');
- }
-
- self.roles = data.entities;
-
- if (typeof(callback) === 'function') {
- callback(err, data, data.entities);
- }
- });
-
+Usergrid.Entity.prototype.getRoles = function(callback) {
+ var self = this;
+ var endpoint = this.get("type") + "/" + this.get("uuid") + "/roles";
+ var options = {
+ method: "GET",
+ endpoint: endpoint
+ };
+ this._client.request(options, function(err, data) {
+ if (err && self._client.logging) {
+ console.log("could not get user roles");
+ }
+ self.roles = data.entities;
+ if (typeof callback === "function") {
+ callback(err, data, data.entities);
+ }
+ });
};
-Usergrid.Entity.prototype.getPermissions = function (callback) {
-
- var self = this;
-
- var endpoint = this.get('type') + '/' + this.get('uuid') + '/permissions' ;
- var options = {
- method:'GET',
- endpoint:endpoint
- };
- this._client.request(options, function (err, data) {
- if (err && self._client.logging) {
- console.log('could not get user permissions');
- }
-
- var permissions = [];
- if (data.data) {
- var perms = data.data;
- var count = 0;
-
- for (var i in perms) {
- count++;
- var perm = perms[i];
- var parts = perm.split(':');
- var ops_part = "";
- var path_part = parts[0];
-
- if (parts.length > 1) {
- ops_part = parts[0];
- path_part = parts[1];
- }
-
- ops_part.replace("*", "get,post,put,delete")
- var ops = ops_part.split(',');
- var ops_object = {}
- ops_object.get = 'no';
- ops_object.post = 'no';
- ops_object.put = 'no';
- ops_object.delete = 'no';
- for (var j in ops) {
- ops_object[ops[j]] = 'yes';
- }
-
- permissions.push({
- operations: ops_object,
- path: path_part,
- perm: perm
- });
- }
- }
-
- self.permissions = permissions;
-
- if (typeof(callback) === 'function') {
- callback(err, data, data.entities);
- }
- });
-
+Usergrid.Entity.prototype.getPermissions = function(callback) {
+ var self = this;
+ var endpoint = this.get("type") + "/" + this.get("uuid") + "/permissions";
+ var options = {
+ method: "GET",
+ endpoint: endpoint
+ };
+ this._client.request(options, function(err, data) {
+ if (err && self._client.logging) {
+ console.log("could not get user permissions");
+ }
+ var permissions = [];
+ if (data.data) {
+ var perms = data.data;
+ var count = 0;
+ for (var i in perms) {
+ count++;
+ var perm = perms[i];
+ var parts = perm.split(":");
+ var ops_part = "";
+ var path_part = parts[0];
+ if (parts.length > 1) {
+ ops_part = parts[0];
+ path_part = parts[1];
+ }
+ ops_part.replace("*", "get,post,put,delete");
+ var ops = ops_part.split(",");
+ var ops_object = {};
+ ops_object.get = "no";
+ ops_object.post = "no";
+ ops_object.put = "no";
+ ops_object.delete = "no";
+ for (var j in ops) {
+ ops_object[ops[j]] = "yes";
+ }
+ permissions.push({
+ operations: ops_object,
+ path: path_part,
+ perm: perm
+ });
+ }
+ }
+ self.permissions = permissions;
+ if (typeof callback === "function") {
+ callback(err, data, data.entities);
+ }
+ });
};
/*
@@ -1494,52 +1460,48 @@ Usergrid.Entity.prototype.getPermissions = function (callback) {
* @return {callback} callback(err, data)
*
*/
-Usergrid.Entity.prototype.disconnect = function (connection, entity, callback) {
-
- var self = this;
-
- var error;
- //connectee info
- var connecteeType = entity.get('type');
- var connectee = this.getEntityId(entity);
- if (!connectee) {
- if (typeof(callback) === 'function') {
- error = 'Error trying to delete object - no uuid specified.';
- if (self._client.logging) {
- console.log(error);
- }
- callback(true, error);
- }
- return;
- }
-
- //connector info
- var connectorType = this.get('type');
- var connector = this.getEntityId(this);
- if (!connector) {
- if (typeof(callback) === 'function') {
- error = 'Error in connect - no uuid specified.';
- if (self._client.logging) {
- console.log(error);
- }
- callback(true, error);
- }
- return;
- }
-
- var endpoint = connectorType + '/' + connector + '/' + connection + '/' + connecteeType + '/' + connectee;
- var options = {
- method:'DELETE',
- endpoint:endpoint
- };
- this._client.request(options, function (err, data) {
- if (err && self._client.logging) {
- console.log('entity could not be disconnected');
- }
- if (typeof(callback) === 'function') {
- callback(err, data);
+Usergrid.Entity.prototype.disconnect = function(connection, entity, callback) {
+ var self = this;
+ var error;
+ //connectee info
+ var connecteeType = entity.get("type");
+ var connectee = this.getEntityId(entity);
+ if (!connectee) {
+ if (typeof callback === "function") {
+ error = "Error trying to delete object - no uuid specified.";
+ if (self._client.logging) {
+ console.log(error);
+ }
+ callback(true, error);
+ }
+ return;
+ }
+ //connector info
+ var connectorType = this.get("type");
+ var connector = this.getEntityId(this);
+ if (!connector) {
+ if (typeof callback === "function") {
+ error = "Error in connect - no uuid specified.";
+ if (self._client.logging) {
+ console.log(error);
+ }
+ callback(true, error);
+ }
+ return;
}
- });
+ var endpoint = connectorType + "/" + connector + "/" + connection + "/" + connecteeType + "/" + connectee;
+ var options = {
+ method: "DELETE",
+ endpoint: endpoint
+ };
+ this._client.request(options, function(err, data) {
+ if (err && self._client.logging) {
+ console.log("entity could not be disconnected");
+ }
+ if (typeof callback === "function") {
+ callback(err, data);
+ }
+ });
};
/*
@@ -1553,87 +1515,77 @@ Usergrid.Entity.prototype.disconnect = function (connection, entity, callback) {
* @return {callback} callback(err, data)
*/
Usergrid.Collection = function(options, callback) {
-
- if (options) {
- this._client = options.client;
- this._type = options.type;
- this.qs = options.qs || {};
-
- //iteration
- this._list = options.list || [];
- this._iterator = options.iterator || -1; //first thing we do is increment, so set to -1
-
- //paging
- this._previous = options.previous || [];
- this._next = options.next || null;
- this._cursor = options.cursor || null;
-
- //restore entities if available
- if (options.list) {
- var count = options.list.length;
- for(var i=0;i<count;i++){
- //make new entity with
- var entity = this._client.restoreEntity(options.list[i]);
- this._list[i] = entity;
- }
+ if (options) {
+ this._client = options.client;
+ this._type = options.type;
+ this.qs = options.qs || {};
+ //iteration
+ this._list = options.list || [];
+ this._iterator = options.iterator || -1;
+ //first thing we do is increment, so set to -1
+ //paging
+ this._previous = options.previous || [];
+ this._next = options.next || null;
+ this._cursor = options.cursor || null;
+ //restore entities if available
+ if (options.list) {
+ var count = options.list.length;
+ for (var i = 0; i < count; i++) {
+ //make new entity with
+ var entity = this._client.restoreEntity(options.list[i]);
+ this._list[i] = entity;
+ }
+ }
+ }
+ if (callback) {
+ //populate the collection
+ this.fetch(callback);
}
- }
- if (callback) {
- //populate the collection
- this.fetch(callback);
- }
-
};
-
/*
* gets the data from the collection object for serialization
*
* @method serialize
* @return {object} data
*/
-Usergrid.Collection.prototype.serialize = function () {
-
- //pull out the state from this object and return it
- var data = {}
- data.type = this._type;
- data.qs = this.qs;
- data.iterator = this._iterator;
- data.previous = this._previous;
- data.next = this._next;
- data.cursor = this._cursor;
-
- this.resetEntityPointer();
- var i=0;
- data.list = [];
- while(this.hasNextEntity()) {
- var entity = this.getNextEntity();
- data.list[i] = entity.serialize();
- i++;
- }
-
- data = JSON.stringify(data);
- return data;
-};
-
-Usergrid.Collection.prototype.addCollection = function (collectionName, options, callback) {
- self = this;
- options.client = this._client;
- var collection = new Usergrid.Collection(options, function(err, data) {
- if (typeof(callback) === 'function') {
-
- collection.resetEntityPointer();
- while(collection.hasNextEntity()) {
- var user = collection.getNextEntity();
- var email = user.get('email');
- var image = self._client.getDisplayImage(user.get('email'), user.get('picture'));
- user._portal_image_icon = image;
- }
-
- self[collectionName] = collection;
- callback(err, collection);
- }
- });
+Usergrid.Collection.prototype.serialize = function() {
+ //pull out the state from this object and return it
+ var data = {};
+ data.type = this._type;
+ data.qs = this.qs;
+ data.iterator = this._iterator;
+ data.previous = this._previous;
+ data.next = this._next;
+ data.cursor = this._cursor;
+ this.resetEntityPointer();
+ var i = 0;
+ data.list = [];
+ while (this.hasNextEntity()) {
+ var entity = this.getNextEntity();
+ data.list[i] = entity.serialize();
+ i++;
+ }
+ data = JSON.stringify(data);
+ return data;
+};
+
+Usergrid.Collection.prototype.addCollection = function(collectionName, options, callback) {
+ self = this;
+ options.client = this._client;
+ var collection = new Usergrid.Collection(options, function(err, data) {
+ if (typeof callback === "function") {
+ collection.resetEntityPointer();
+ while (collection.hasNextEntity()) {
+ var user = collection.getNextEntity();
+ var email = user.get("email");
+ var image = self._client.getDisplayImage(user.get("email"), user.get("picture"));
+ user._portal_image_icon = image;
+ }
+ self[collectionName] = collection;
+ callback(err, collection);
+ }
+ });
};
/*
@@ -1643,58 +1595,59 @@ Usergrid.Collection.prototype.addCollection = function (collectionName, options,
* @param {function} callback
* @return {callback} callback(err, data)
*/
-Usergrid.Collection.prototype.fetch = function (callback) {
- var self = this;
- var qs = this.qs;
-
- //add in the cursor if one is available
- if (this._cursor) {
- qs.cursor = this._cursor;
- } else {
- delete qs.cursor;
- }
- var options = {
- method:'GET',
- endpoint:this._type,
- qs:this.qs
- };
- this._client.request(options, function (err, data) {
- if(err && self._client.logging) {
- console.log('error getting collection');
+Usergrid.Collection.prototype.fetch = function(callback) {
+ var self = this;
+ var qs = this.qs;
+ //add in the cursor if one is available
+ if (this._cursor) {
+ qs.cursor = this._cursor;
} else {
- //save the cursor if there is one
- var cursor = data.cursor || null;
- self.saveCursor(cursor);
- if (data.entities) {
- self.resetEntityPointer();
- var count = data.entities.length;
- //save entities locally
- self._list = []; //clear the local list first
- for (var i=0;i<count;i++) {
- var uuid = data.entities[i].uuid;
- if (uuid) {
- var entityData = data.entities[i] || {};
- self._baseType = data.entities[i].type; //store the base type in the collection
- entityData.type = self._type;//make sure entities are same type (have same path) as parent collection.
- var entityOptions = {
- type:self._type,
- client:self._client,
- uuid:uuid,
- data:entityData
- };
-
- var ent = new Usergrid.Entity(entityOptions);
- ent._json = JSON.stringify(entityData, null, 2);
- var ct = self._list.length;
- self._list[ct] = ent;
- }
- }
- }
- }
- if (typeof(callback) === 'function') {
- callback(err, data);
+ delete qs.cursor;
}
- });
+ var options = {
+ method: "GET",
+ endpoint: this._type,
+ qs: this.qs
+ };
+ this._client.request(options, function(err, data) {
+ if (err && self._client.logging) {
+ console.log("error getting collection");
+ } else {
+ //save the cursor if there is one
+ var cursor = data.cursor || null;
+ self.saveCursor(cursor);
+ if (data.entities) {
+ self.resetEntityPointer();
+ var count = data.entities.length;
+ //save entities locally
+ self._list = [];
+ //clear the local list first
+ for (var i = 0; i < count; i++) {
+ var uuid = data.entities[i].uuid;
+ if (uuid) {
+ var entityData = data.entities[i] || {};
+ self._baseType = data.entities[i].type;
+ //store the base type in the collection
+ entityData.type = self._type;
+ //make sure entities are same type (have same path) as parent collection.
+ var entityOptions = {
+ type: self._type,
+ client: self._client,
+ uuid: uuid,
+ data: entityData
+ };
+ var ent = new Usergrid.Entity(entityOptions);
+ ent._json = JSON.stringify(entityData, null, 2);
+ var ct = self._list.length;
+ self._list[ct] = ent;
+ }
+ }
+ }
+ }
+ if (typeof callback === "function") {
+ callback(err, data);
+ }
+ });
};
/*
@@ -1705,27 +1658,26 @@ Usergrid.Collection.prototype.fetch = function (callback) {
* @param {function} callback
* @return {callback} callback(err, data, entity)
*/
-Usergrid.Collection.prototype.addEntity = function (options, callback) {
- var self = this;
- options.type = this._type;
-
- //create the new entity
- this._client.createEntity(options, function (err, entity) {
- if (!err) {
- //then add the entity to the list
- var count = self._list.length;
- self._list[count] = entity;
- }
- if (typeof(callback) === 'function') {
- callback(err, entity);
- }
- });
+Usergrid.Collection.prototype.addEntity = function(options, callback) {
+ var self = this;
+ options.type = this._type;
+ //create the new entity
+ this._client.createEntity(options, function(err, entity) {
+ if (!err) {
+ //then add the entity to the list
+ var count = self._list.length;
+ self._list[count] = entity;
+ }
+ if (typeof callback === "function") {
+ callback(err, entity);
+ }
+ });
};
-Usergrid.Collection.prototype.addExistingEntity = function (entity) {
- //entity should already exist in the db, so just add it to the list
- var count = this._list.length;
- this._list[count] = entity;
+Usergrid.Collection.prototype.addExistingEntity = function(entity) {
+ //entity should already exist in the db, so just add it to the list
+ var count = this._list.length;
+ this._list[count] = entity;
};
/*
@@ -1736,35 +1688,34 @@ Usergrid.Collection.prototype.addExistingEntity = function (entity) {
* @param {function} callback
* @return {callback} callback(err, data)
*/
-Usergrid.Collection.prototype.destroyEntity = function (entity, callback) {
- var self = this;
- entity.destroy(function(err, data) {
- if (err) {
- if (self._client.logging) {
- console.log('could not destroy entity');
- }
- if (typeof(callback) === 'function') {
- callback(err, data);
- }
- } else {
- //destroy was good, so repopulate the collection
- self.fetch(callback);
- }
- });
- //remove entity from the local store
- this.removeEntity(entity);
+Usergrid.Collection.prototype.destroyEntity = function(entity, callback) {
+ var self = this;
+ entity.destroy(function(err, data) {
+ if (err) {
+ if (self._client.logging) {
+ console.log("could not destroy entity");
+ }
+ if (typeof callback === "function") {
+ callback(err, data);
+ }
+ } else {
+ //destroy was good, so repopulate the collection
+ self.fetch(callback);
+ }
+ });
+ //remove entity from the local store
+ this.removeEntity(entity);
};
-
-Usergrid.Collection.prototype.removeEntity = function (entity) {
- var uuid = entity.get('uuid');
- for (var key in this._list) {
- var listItem = this._list[key];
- if (listItem.get('uuid') === uuid) {
- return this._list.splice(key, 1);
+Usergrid.Collection.prototype.removeEntity = function(entity) {
+ var uuid = entity.get("uuid");
+ for (var key in this._list) {
+ var listItem = this._list[key];
+ if (listItem.get("uuid") === uuid) {
+ return this._list.splice(key, 1);
+ }
}
- }
- return false;
+ return false;
};
/*
@@ -1775,25 +1726,23 @@ Usergrid.Collection.prototype.removeEntity = function (entity) {
* @param {function} callback
* @return {callback} callback(err, data, entity)
*/
-Usergrid.Collection.prototype.getEntityByUUID = function (uuid, callback) {
-
- for (var key in this._list) {
- var listItem = this._list[key];
- if (listItem.get('uuid') === uuid) {
- return listItem;
+Usergrid.Collection.prototype.getEntityByUUID = function(uuid, callback) {
+ for (var key in this._list) {
+ var listItem = this._list[key];
+ if (listItem.get("uuid") === uuid) {
+ return listItem;
+ }
}
- }
-
- //get the entity from the database
- var options = {
- data: {
- type: this._type,
- uuid:uuid
- },
- client: this._client
- }
- var entity = new Usergrid.Entity(options);
- entity.fetch(callback);
+ //get the entity from the database
+ var options = {
+ data: {
+ type: this._type,
+ uuid: uuid
+ },
+ client: this._client
+ };
+ var entity = new Usergrid.Entity(options);
+ entity.fetch(callback);
};
/*
@@ -1802,12 +1751,12 @@ Usergrid.Collection.prototype.getEntityByUUID = function (uuid, callback) {
* @method getFirstEntity
* @return {object} returns an entity object
*/
-Usergrid.Collection.prototype.getFirstEntity = function () {
- var count = this._list.length;
- if (count > 0) {
- return this._list[0];
- }
- return null;
+Usergrid.Collection.prototype.getFirstEntity = function() {
+ var count = this._list.length;
+ if (count > 0) {
+ return this._list[0];
+ }
+ return null;
};
/*
@@ -1816,12 +1765,12 @@ Usergrid.Collection.prototype.getFirstEntity = function () {
* @method getLastEntity
* @return {object} returns an entity object
*/
-Usergrid.Collection.prototype.getLastEntity = function () {
- var count = this._list.length;
- if (count > 0) {
- return this._list[count-1];
- }
- return null;
+Usergrid.Collection.prototype.getLastEntity = function() {
+ var count = this._list.length;
+ if (count > 0) {
+ return this._list[count - 1];
+ }
+ return null;
};
/*
@@ -1833,13 +1782,13 @@ Usergrid.Collection.prototype.getLastEntity = function () {
* @method hasNextEntity
* @return {boolean} true if there is a next entity, false if not
*/
-Usergrid.Collection.prototype.hasNextEntity = function () {
- var next = this._iterator + 1;
- var hasNextElement = (next >=0 && next < this._list.l
<TRUNCATED>