weight.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. angular.module("robware").controller("weight", ["$scope", "weightService", function($scope, weightService) {
  2. $scope.submitting = false;
  3. $scope.weight = null;
  4. $scope.fat = null;
  5. $scope.readings = [];
  6. $scope.headings = {weight: "Weight (KG)", fat: "Fat %", bmi: "BMI"};
  7. function refreshReadings() {
  8. weightService.getReadings().then(function(data) {
  9. $scope.readings = data;
  10. });
  11. }
  12. $scope.$watch("readings", function() {
  13. for (var i = 0; i < $scope.readings.length; i++) {
  14. $scope.readings[i].date = new Date($scope.readings[i].date);
  15. }
  16. });
  17. $scope.addReading = function() {
  18. if (!$scope.weight || !$scope.fat)
  19. return;
  20. $scope.submitting = true;
  21. weightService.addWeight($scope.weight, $scope.fat).then(function(data) {
  22. refreshReadings();
  23. $scope.submitting = false;
  24. $scope.weight = null;
  25. $scope.fat = null;
  26. });
  27. };
  28. $scope.deleteReading = function(id) {
  29. weightService.deleteWeight(id).then(function() {
  30. refreshReadings();
  31. });
  32. };
  33. Object.defineProperty($scope, "tenLatestReadings", {
  34. get: function() {
  35. var temp = $scope.readings.slice(-10);
  36. temp.reverse();
  37. return temp;
  38. }
  39. });
  40. }]);