API Docs for: 0.9.1
Show:

File: lib/service/endPoint.js

  1. var ServiceObject = require('./abstract/serviceObject');
  2.  
  3. var ServiceNewEntry = require('./newEntry');
  4. var ServiceEntry = require('./entry');
  5. var ServiceList = require('./list');
  6.  
  7. var _ = require('underscore');
  8.  
  9. /**
  10. * @class ServiceEndPoint
  11. * @extends ServiceObject
  12. *
  13. * @property {string} _moduleName
  14. */
  15. var ServiceEndPoint = ServiceObject.extend(
  16.  
  17. {
  18. /**
  19. * Gets the singular module identifier
  20. *
  21. * @method getSingularModuleName
  22. * @return {string}
  23. */
  24. getSingularModuleName: function () {
  25. return this.constructor.moduleNames.singular;
  26. },
  27.  
  28. /**
  29. * Gets the plural module identifier
  30. *
  31. * @method getPluralModuleName
  32. * @return {string}
  33. */
  34. getPluralModuleName: function () {
  35. return this.constructor.moduleNames.plural;
  36. },
  37.  
  38.  
  39. /**
  40. * Gets a list of fields available
  41. *
  42. * @method getFields
  43. * @return {string[]}
  44. */
  45. getFields: function () {
  46. return _.keys(this.constructor.fields);
  47. },
  48.  
  49. /**
  50. * Checks if a field is available
  51. *
  52. * @method hasField
  53. * @param {string} field
  54. * @return {boolean}
  55. */
  56. hasField: function (field) {
  57. return (this.getFields().indexOf(field) !== -1);
  58. },
  59.  
  60. /**
  61. * Gets details field information
  62. *
  63. * @method getFieldInfo
  64. * @param {string} field
  65. * @return {object}
  66. */
  67. getFieldInfo: function (field) {
  68.  
  69. if (!this.hasField(field)) {
  70. throw new Error('Field ' + field + ' cannot be found');
  71. }
  72.  
  73. return this.constructor.fields[field];
  74. },
  75.  
  76.  
  77. /**
  78. * Retrieves a new entry object
  79. *
  80. * @method newEntry
  81. * @param {object} [entry]
  82. * @return {ServiceEntry}
  83. */
  84. newEntry: function (entry) {
  85. var Class = this.constructor.objects.NewEntry;
  86. return new Class(entry, this._getChildOptions());
  87. },
  88.  
  89. /**
  90. * Retrieves a list object
  91. *
  92. * @method loadList
  93. * @param {ServiceEntry[]} items
  94. * @param {object} [options]
  95. * @return {ServiceList}
  96. */
  97. loadList: function (items, options) {
  98. var localOptions = this._getChildOptions(),
  99. Class = this.constructor.objects.List;
  100.  
  101. localOptions.container = options.container;
  102. return new Class(items, localOptions);
  103. },
  104.  
  105. /**
  106. * Retrieves an entry object
  107. *
  108. * @method loadEntry
  109. * @param {object} entry
  110. * @param {object} [options]
  111. * @return {ServiceEntry}
  112. */
  113. loadEntry: function (entry, options) {
  114. var localOptions = this._getChildOptions(),
  115. Class = this.constructor.objects.Entry;
  116.  
  117. localOptions.container = options.container;
  118. return new Class(entry, localOptions);
  119. },
  120.  
  121.  
  122. /**
  123. * Gets the options for children
  124. *
  125. * @method _getChildOptions
  126. * @return {object}
  127. * @private
  128. */
  129. _getChildOptions: function () {
  130. return {
  131. request: this.getRequest(),
  132. service: this.getService(),
  133. endPoint: this
  134. };
  135. },
  136.  
  137.  
  138. /**
  139. * Removes an item
  140. *
  141. * @method removeItem
  142. * @param {string} id
  143. * @return {Promise} With {string} id
  144. */
  145. removeItem: function (id) {
  146. var url = encodeURIComponent(this.getPluralModuleName()) + '/' + encodeURIComponent(id);
  147. this.getRequest().sendRequest("DELETE", url).then(function () {
  148. return id;
  149. });
  150. },
  151.  
  152. /**
  153. * Gets a list of items
  154. *
  155. * @method getAllItems
  156. * @param {object} [options]
  157. * @return {Promise} With {ServiceList}
  158. */
  159. getAllItems: function (options) {
  160. var url = encodeURIComponent(this.getPluralModuleName()),
  161. components = [],
  162. localOptions = options || {},
  163. keys = _.keys(localOptions);
  164.  
  165. if (keys.length > 0) {
  166. url += '?';
  167. }
  168. _.each(keys, function (key) {
  169. components.push(encodeURIComponent(key) + '=' + encodeURIComponent(localOptions[key]));
  170. }, this);
  171. if (keys.length > 0) {
  172. url += components.join('&');
  173. }
  174.  
  175. return this.getRequest().sendRequest("GET", url).then(function (data) {
  176. var container = this.getService().parse(data),
  177. moduleName = this.getPluralModuleName(),
  178. moduleList = container.getModuleList(moduleName);
  179.  
  180. return moduleList;
  181. }.bind(this));
  182. },
  183.  
  184. /**
  185. * Gets an item
  186. *
  187. * @method getItem
  188. * @param {string} id
  189. * @return {Promise} With {ServiceEntry}
  190. */
  191. getItem: function (id) {
  192. var url = encodeURIComponent(this.getPluralModuleName()) + '/' + encodeURIComponent(id);
  193.  
  194. return this.getRequest().sendRequest("GET", url).then(function (data) {
  195. var container = this.getService().parse(data),
  196. moduleName = this.getPluralModuleName(),
  197. moduleList = container.getModuleList(moduleName);
  198.  
  199. return moduleList.getItem(id);
  200. }.bind(this));
  201. },
  202.  
  203. /**
  204. * Queries a list of items
  205. *
  206. * @method queryItems
  207. * @param {string} query
  208. * @param {object} [options]
  209. * @return {Promise} With {ServiceList}
  210. */
  211. queryItems: function (query, options) {
  212. var url = encodeURIComponent(this.getPluralModuleName()) + '?',
  213. components = [],
  214. localOptions = options || {
  215. query: query
  216. },
  217. keys = _.keys(localOptions);
  218.  
  219. keys.push('query');
  220. _.each(keys, function (key) {
  221. components.push(encodeURIComponent(key) + '=' + encodeURIComponent(localOptions[key]));
  222. }, this);
  223. if (keys.length > 0) {
  224. url += components.join('&');
  225. }
  226.  
  227. return this.getRequest().sendRequest("GET", url).then(function (data) {
  228. var container = this.getService().parse(data),
  229. moduleName = this.getPluralModuleName(),
  230. moduleList = container.getModuleList(moduleName);
  231.  
  232. return moduleList;
  233. }.bind(this));
  234. }
  235. },
  236. {
  237. /**
  238. * @property TYPE
  239. * @type {string}
  240. * @static
  241. */
  242. TYPE: 'ServiceEndPoint',
  243.  
  244. moduleNames: {
  245. singular: null,
  246. plural: null
  247. },
  248.  
  249. objects: {
  250. NewEntry: ServiceNewEntry,
  251. Entry: ServiceEntry,
  252. List: ServiceList
  253. },
  254.  
  255. fields: {},
  256.  
  257. RELATION_ONE_TO_ONE: 'oneToOne',
  258. RELATION_ONE_TO_MANY: 'oneToMany'
  259. }
  260. );
  261.  
  262. module.exports = ServiceEndPoint;
  263.