/js/apps/system/_admin/aardvark/APP/frontend/js/collections/arangoDocument.js

https://github.com/triAGENS/ArangoDB · JavaScript · 232 lines · 199 code · 25 blank · 8 comment · 21 complexity · 57ff50c17e1c4a8863a02c446ecc7bb6 MD5 · raw file

  1. /* jshint browser: true */
  2. /* jshint strict: false, unused: false */
  3. /* global Backbone, window, arangoDocumentModel, $, arangoHelper */
  4. window.ArangoDocument = Backbone.Collection.extend({
  5. url: '/_api/document/',
  6. model: arangoDocumentModel,
  7. collectionInfo: {},
  8. deleteEdge: function (colid, dockey, callback) {
  9. this.deleteDocument(colid, dockey, callback);
  10. },
  11. deleteDocument: function (colid, dockey, callback) {
  12. var data = {
  13. keys: [dockey],
  14. collection: colid
  15. };
  16. $.ajax({
  17. cache: false,
  18. type: 'PUT',
  19. contentType: 'application/json',
  20. data: JSON.stringify(data),
  21. url: arangoHelper.databaseUrl('/_api/simple/remove-by-keys'),
  22. success: function () {
  23. callback(false);
  24. },
  25. error: function () {
  26. callback(true);
  27. }
  28. });
  29. },
  30. addDocument: function (collectionID, key, body) {
  31. this.createTypeDocument(collectionID, key, body);
  32. },
  33. createTypeEdge: function (collectionID, from, to, key, body, callback) {
  34. var newEdge;
  35. try {
  36. if (key) {
  37. body._key = key;
  38. }
  39. body._from = from;
  40. body._to = to;
  41. newEdge = JSON.stringify(body);
  42. }
  43. catch (x) {
  44. body = {};
  45. return this.createTypeEdge(collectionID, from, to, key, body, callback);
  46. }
  47. $.ajax({
  48. cache: false,
  49. type: 'POST',
  50. url: arangoHelper.databaseUrl('/_api/document?collection=' + encodeURIComponent(collectionID)),
  51. data: newEdge,
  52. contentType: 'application/json',
  53. processData: false,
  54. success: function (data) {
  55. callback(false, data._id);
  56. },
  57. error: function (data) {
  58. callback(true, data._id, data.responseJSON);
  59. }
  60. });
  61. },
  62. createTypeDocument: function (collectionID, key, body, callback, returnNew,
  63. smartJoinAttribute, smartJoinAttributeValue,
  64. smartGraphAttribute, smartGraphAttributeValue) {
  65. var newDocument = body;
  66. try {
  67. if (smartJoinAttribute && smartJoinAttributeValue && key) {
  68. // case: smartJoin, bot value are needed and NOT optional
  69. newDocument._key = smartJoinAttributeValue + ':' + key;
  70. newDocument[smartJoinAttribute] = smartJoinAttributeValue;
  71. } else if (smartGraphAttribute && smartGraphAttributeValue) {
  72. // case: smartGraph with value
  73. // other to smartJoin, we can:
  74. // 1.) Create without smartGraphAttribute and without smartGraphAttributeValue
  75. // 2.) Create only with smartGraphAttributeValue
  76. if (key) {
  77. newDocument._key = smartGraphAttributeValue + ':' + key;
  78. }
  79. newDocument[smartGraphAttribute] = smartGraphAttributeValue;
  80. } else if (key) {
  81. newDocument._key = key;
  82. }
  83. }
  84. catch (x) {
  85. body = {};
  86. return this.createTypeDocument(collectionID, key, body, callback, returnNew,
  87. smartJoinAttribute, smartJoinAttributeValue,
  88. smartGraphAttribute, smartGraphAttributeValue);
  89. }
  90. newDocument = JSON.stringify(newDocument);
  91. var url = arangoHelper.databaseUrl('/_api/document?collection=' + encodeURIComponent(collectionID));
  92. if (returnNew) {
  93. url = url + '?returnNew=true';
  94. }
  95. $.ajax({
  96. cache: false,
  97. type: 'POST',
  98. url: url,
  99. data: newDocument,
  100. contentType: 'application/json',
  101. processData: false,
  102. success: function (data) {
  103. if (returnNew) {
  104. callback(false, data);
  105. } else {
  106. callback(false, data._id);
  107. }
  108. },
  109. error: function (data) {
  110. callback(true, data._id, data.responseJSON);
  111. }
  112. });
  113. },
  114. getCollectionInfo: function (identifier, callback, toRun) {
  115. var self = this;
  116. $.ajax({
  117. cache: false,
  118. type: 'GET',
  119. url: arangoHelper.databaseUrl('/_api/collection/' + encodeURIComponent(identifier) + '?' + arangoHelper.getRandomToken()),
  120. contentType: 'application/json',
  121. processData: false,
  122. success: function (data) {
  123. self.collectionInfo = data;
  124. callback(false, data, toRun);
  125. },
  126. error: function (data) {
  127. callback(true, data, toRun);
  128. }
  129. });
  130. },
  131. getEdge: function (colid, dockey, callback) {
  132. this.getDocument(colid, dockey, callback);
  133. },
  134. getDocument: function (colid, dockey, callback) {
  135. var self = this;
  136. this.clearDocument();
  137. var data = {
  138. keys: [dockey],
  139. collection: colid
  140. };
  141. $.ajax({
  142. cache: false,
  143. type: 'PUT',
  144. url: arangoHelper.databaseUrl('/_api/simple/lookup-by-keys'),
  145. contentType: 'application/json',
  146. data: JSON.stringify(data),
  147. processData: false,
  148. success: function (data) {
  149. if (data.documents[0]) {
  150. self.add(data.documents[0]);
  151. if (data.documents[0]._from && data.documents[0]._to) {
  152. callback(false, data, 'edge');
  153. } else {
  154. callback(false, data, 'document');
  155. }
  156. } else {
  157. self.add(true, data);
  158. callback(true, data, colid + '/' + dockey);
  159. }
  160. },
  161. error: function (data) {
  162. self.add(true, data);
  163. arangoHelper.arangoError('Error', data.responseJSON.errorMessage + ' - error number: ' + data.responseJSON.errorNum);
  164. callback(true, data, colid + '/' + dockey);
  165. }
  166. });
  167. },
  168. saveEdge: function (colid, dockey, from, to, model, callback) {
  169. this.saveDocument(colid, dockey, model, callback, from, to);
  170. },
  171. saveDocument: function (colid, dockey, model, callback, from, to) {
  172. if (typeof model === 'string') {
  173. try {
  174. model = JSON.parse(model);
  175. } catch (e) {
  176. arangoHelper.arangoError('Could not parse document: ' + model);
  177. }
  178. }
  179. model._key = dockey;
  180. model._id = colid + '/' + dockey;
  181. if (from && to) {
  182. model._from = from;
  183. model._to = to;
  184. }
  185. $.ajax({
  186. cache: false,
  187. type: 'PUT',
  188. url: arangoHelper.databaseUrl('/_api/document/' + encodeURIComponent(colid) + '?returnNew=true'),
  189. data: JSON.stringify([model]),
  190. contentType: 'application/json',
  191. processData: false,
  192. success: function (data) {
  193. callback(false, data);
  194. },
  195. error: function (data) {
  196. callback(true, data);
  197. }
  198. });
  199. },
  200. updateLocalDocument: function (data) {
  201. this.clearDocument();
  202. this.add(data);
  203. },
  204. clearDocument: function () {
  205. this.reset();
  206. }
  207. });