ces.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. var CES = {};
  2. (function() {
  3. CES.allEntities = function(cb) {
  4. $.getJSON('/entities', function(data, status) {
  5. if(status != 'success') {
  6. console.log("failed to fetch all entities with status code " + status);
  7. cb(err);
  8. }
  9. //console.log(data);
  10. cb(null, data);
  11. })
  12. };
  13. CES.fetchEntity = function(eid, cb) {
  14. $.getJSON('/fetchEntity', {eid: eid}, function(data, status) {
  15. if(status != 'success') {
  16. console.log("failed to fetch entity #"+eid+" with status code " + status);
  17. cb(err);
  18. }
  19. console.log('full entity: ', data)
  20. cb(null, data);
  21. })
  22. };
  23. CES.upsertEntity = function(ent, cb) {
  24. $.getJSON('/upsertEntity', ent, function(data, status) {
  25. console.log("getjson callback for upsert entity")
  26. if(status != 'success') {
  27. console.log("failed to upsert entity #"+ent.eid+" with status code " + status);
  28. cb(err);
  29. }
  30. cb(null, data);
  31. })
  32. };
  33. CES.createEntity = function(ent, cb) {
  34. $.getJSON('/createEntity', ent, function(data, status) {
  35. console.log("getjson callback for create entity")
  36. if(status != 'success') {
  37. console.log("failed to create entity #"+ent.eid+" with status code " + status);
  38. cb(err);
  39. }
  40. cb(null, data);
  41. })
  42. };
  43. CES.deleteEntity = function(eid, cb) {
  44. $.getJSON('/deleteEntity', {eid: eid}, function(data, status) {
  45. if(status != 'success') {
  46. cb(status);
  47. return;
  48. }
  49. cb(null, data);
  50. });
  51. };
  52. CES.upsertComponents = function(eid, components, cb) {
  53. var ent = Object.assign({}, components, {eid: eid});
  54. $.getJSON('/upsertComponents', ent, function(data, status) {
  55. if(status != 'success') {
  56. console.log("failed to upsert entity #"+eid+" with status code " + status);
  57. cb(err);
  58. }
  59. cb(null, data);
  60. })
  61. };
  62. var types = null;
  63. CES.listTypes = function(cb) {
  64. if(types !== null) return cb(null, types);
  65. $.getJSON('/types', function(data, status) {
  66. if(status != 'success') {
  67. console.log("failed to fetch entity types with status code " + status);
  68. cb(status);
  69. }
  70. types = data;
  71. cb(null, data);
  72. })
  73. };
  74. CES.addType = function(name, dataType, cb) {
  75. $.getJSON('/addType', {name: name, dataType: dataType}, function(data, status) {
  76. if(status != 'success') {
  77. console.log("failed to add type with status code " + status);
  78. cb(status);
  79. }
  80. cb(null, data);
  81. })
  82. };
  83. // this is the main function to use
  84. CES.findEntitiesWithComps = function(compNames, cb) {
  85. $.getJSON('/findEntitiesWithAnyComps', {compNames: compNames}, function(data, status) {
  86. if(status != 'success') {
  87. console.log("failed to fetch entities with comps with status code " + status);
  88. cb(status);
  89. }
  90. cb(null, data);
  91. })
  92. };
  93. CES.findFullEntitiesWithComps = function(compNames, cb) {
  94. $.getJSON('/findFullEntitiesWithComps', compNames, function(data, status) {
  95. if(status != 'success') {
  96. console.log("failed to fetch entities with comps with status code " + status);
  97. cb(status);
  98. }
  99. cb(null, data);
  100. })
  101. };
  102. CES.findEntity = function(compNames, cb) {
  103. $.getJSON('/findEntity', {fields: compNames}, function(data, status) {
  104. if(status != 'success') {
  105. console.log("failed to find entity with status code " + status);
  106. cb(status);
  107. }
  108. cb(null, data);
  109. })
  110. };
  111. })();