basicFoodEditor.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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', 'ndbKey',
  54. function($scope, $uibModalInstance, NDBList, BasicFood, foodData, ndbKey) {
  55. if (foodData == null)
  56. {
  57. console.error("No food data to edit!");
  58. return;
  59. }
  60. $scope.catagories = NDBList.get({
  61. key: ndbKey,
  62. type: "g"
  63. });
  64. $scope.food = foodData;
  65. $scope.submit = function(food) {
  66. food.$save($scope.close, function (err) {
  67. // TODO: Proper error handling
  68. console.error(err);
  69. });
  70. };
  71. $scope.delete = function(food) {
  72. food.$delete($scope.close, function (err) {
  73. // TODO: Proper error handling
  74. console.error(err);
  75. });
  76. };
  77. var initSubmit = function() {
  78. var submitFnName = foodData.id == null? "$save":"$update";
  79. $scope.submit = function(food) {
  80. food[submitFnName]($scope.close, function (err) {
  81. // TODO: Proper error handling
  82. console.error(err);
  83. });
  84. };
  85. };
  86. if (foodData.$promise) {
  87. foodData.$promise.then(initSubmit);
  88. }
  89. else {
  90. initSubmit();
  91. }
  92. // TODO:
  93. // Will this controller ever be used non-modally?
  94. // Will it even work as a non-modal?
  95. $scope.close = $uibModalInstance != null?
  96. $uibModalInstance.close : null;
  97. $scope.dismiss = $uibModalInstance != null?
  98. $uibModalInstance.dismiss : null;
  99. }]);