units.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /**
  2. * Provides interface for unit apis.
  3. */
  4. (function() {
  5. var restResource = function($resource) {
  6. return function() {
  7. arguments[2]["create"] = {
  8. method: "PUT"
  9. };
  10. arguments[2]["update"] = {
  11. method: "POST"
  12. };
  13. var res = $resource.apply(null, arguments);
  14. res.prototype.$save = function() {
  15. return this[this.id? "$update":"$create"].apply(this, arguments);
  16. };
  17. return res;
  18. };
  19. };
  20. angular.module('Units', ['ngResource'])
  21. /*
  22. * Collection of unit apis.
  23. *
  24. * Methods:
  25. * get: Gets information for a unit by id
  26. * query: Get a list of all units
  27. * queryType: Get a list of all units of a type
  28. * delete: Deletes a unit with an id
  29. * create: Creates a unit
  30. * update: Updates a unit
  31. * normalize: Converts from the given unit amount to the primary unit
  32. * save: Creates a new unit or updates an existing unit
  33. */
  34. .factory('Unit', ['$resource', function($resource) {
  35. var res = restResource($resource)("unit/:id", {id: "@id"}, {
  36. "queryType": {
  37. method: 'GET',
  38. isArray: true,
  39. url: "unit/type/:type"
  40. },
  41. "primary": {
  42. method: 'GET',
  43. isArray: true,
  44. url: "unit/primary/:type"
  45. }
  46. });
  47. res.prototype.normalize = function(amount) {
  48. return amount * this.conversion;
  49. };
  50. return res;
  51. }]);
  52. })();