basicFoodEditor.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. angular.module('basicFoodEditor', ['ndbDatabase', 'ngResource'])
  2. .factory('BasicFood', ['$resource', '$q', function($resource, $q) {
  3. return $resource('food/basic/:id', {id: "@id"}, {
  4. 'get': {
  5. url: "food/:id",
  6. method: 'GET'
  7. },
  8. 'query': {
  9. url: "food/basic/query/:query",
  10. method: 'GET',
  11. isArray: true
  12. },
  13. 'save': {method: 'PUT'},
  14. 'update': {method: 'POST'},
  15. 'delete': {
  16. url: "food/:id",
  17. method: 'DELETE'
  18. }
  19. });
  20. }])
  21. .factory('Recipe', ['$resource', '$q', function($resource, $q) {
  22. return $resource('food/recipe/:id', {id: "@id"}, {
  23. 'get': {
  24. url: "food/:id",
  25. method: 'GET'
  26. },
  27. 'query': {
  28. url: "food/recipe/query/:query",
  29. method: 'GET',
  30. isArray: true
  31. },
  32. 'save': {method: 'PUT'},
  33. 'update': {method: 'POST'},
  34. 'delete': {
  35. url: "food/:id",
  36. method: 'DELETE'
  37. }
  38. });
  39. }])
  40. .factory('Food', ['$resource', '$q', function($resource, $q) {
  41. return $resource('food/:id', {id: "@id"}, {
  42. 'get': {method: 'GET'},
  43. 'query': {
  44. url: "food/query/:query/",
  45. method: 'GET',
  46. isArray: true
  47. },
  48. 'delete': {method: 'DELETE'}
  49. });
  50. }])
  51. // TODO: Only ever used with editBasicFood template. See if can auto link.
  52. .controller('BasicFoodEditorController',
  53. ['$scope', '$uibModalInstance', 'NDBList', 'BasicFood', 'foodData',
  54. function($scope, $uibModalInstance, NDBList, BasicFood, foodData) {
  55. if (foodData == null)
  56. {
  57. console.error("No food data to edit!");
  58. return;
  59. }
  60. // TODO: Replace with properties file key.
  61. $scope.catagories = NDBList.get({
  62. key: "CfiHcUnSf0RX0jBuqiWjDK2d2ziOmoZG15CTdhQn",
  63. type: "g"
  64. });
  65. $scope.food = foodData;
  66. $scope.submit = function(food) {
  67. food.$save($scope.close, function (err) {
  68. // TODO: Proper error handling
  69. console.error(err);
  70. });
  71. };
  72. $scope.delete = function(food) {
  73. food.$delete($scope.close, function (err) {
  74. // TODO: Proper error handling
  75. console.error(err);
  76. });
  77. };
  78. var initSubmit = function() {
  79. var submitFnName = foodData.id == null? "$save":"$update";
  80. $scope.submit = function(food) {
  81. food[submitFnName]($scope.close, function (err) {
  82. // TODO: Proper error handling
  83. console.error(err);
  84. });
  85. };
  86. };
  87. if (foodData.$promise) {
  88. foodData.$promise.then(initSubmit);
  89. }
  90. else {
  91. initSubmit();
  92. }
  93. // TODO:
  94. // Will this controller ever be used non-modally?
  95. // Will it even work as a non-modal?
  96. $scope.close = $uibModalInstance != null?
  97. $uibModalInstance.close : null;
  98. $scope.dismiss = $uibModalInstance != null?
  99. $uibModalInstance.dismiss : null;
  100. }]);