basicFoodEditor.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. angular.module('basicFoodEditor', ['ndbDatabase', 'ngResource', 'Food'])
  2. // TODO: Only ever used with editBasicFood template. See if can auto link.
  3. .controller('BasicFoodEditorController',
  4. ['$scope', '$uibModalInstance', 'NDBList', 'BasicFood', 'foodData', 'ndbKey',
  5. function($scope, $uibModalInstance, NDBList, BasicFood, foodData, ndbKey) {
  6. if (foodData == null)
  7. {
  8. console.error("No food data to edit!");
  9. return;
  10. }
  11. $scope.catagories = NDBList.get({
  12. key: ndbKey,
  13. type: "g"
  14. });
  15. $scope.food = foodData;
  16. $scope.submit = function(food) {
  17. food.$save($scope.close, function (err) {
  18. // TODO: Proper error handling
  19. console.error(err);
  20. });
  21. };
  22. $scope.delete = function(food) {
  23. food.$delete($scope.close, function (err) {
  24. // TODO: Proper error handling
  25. console.error(err);
  26. });
  27. };
  28. var initSubmit = function() {
  29. var submitFnName = foodData.id == null? "$save":"$update";
  30. $scope.submit = function(food) {
  31. food[submitFnName]($scope.close, function (err) {
  32. // TODO: Proper error handling
  33. console.error(err);
  34. });
  35. };
  36. };
  37. if (foodData.$promise) {
  38. foodData.$promise.then(initSubmit);
  39. }
  40. else {
  41. initSubmit();
  42. }
  43. // TODO:
  44. // Will this controller ever be used non-modally?
  45. // Will it even work as a non-modal?
  46. $scope.close = $uibModalInstance != null?
  47. $uibModalInstance.close : null;
  48. $scope.dismiss = $uibModalInstance != null?
  49. $uibModalInstance.dismiss : null;
  50. }]);