category.js 948 B

123456789101112131415161718192021222324252627282930
  1. angular.module('NoteWrangler').factory('Category', function CategoryFactory($http, $q) {
  2. var categories;
  3. return {
  4. all: function() {
  5. var deferred = $q.defer();
  6. /*
  7. Since categories hardly ever change, we want to cache the value after it's been fetched
  8. once. We use a promise here so that we intercept the value from the $http service and
  9. save it. If the value has been saved we can resolve with `categories` if not we make
  10. the ajax call with $http and resolve the promise with the result.
  11. */
  12. if(categories) {
  13. deferred.resolve(categories);
  14. } else {
  15. $http({method: 'GET', url: "/categories"})
  16. .success(function(data) {
  17. categories = data;
  18. deferred.resolve(data);
  19. })
  20. .error(function(err) {
  21. deferred.reject(err)
  22. });
  23. }
  24. return deferred.promise;
  25. }
  26. };
  27. });