Advertisement
Guest User

Untitled

a guest
Apr 12th, 2014
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*******************/
  2. /***** GLOBAL ******/
  3. /*******************/
  4.  
  5. var places = angular.module('places',['ngCookies']);
  6.  
  7. places.config(function($interpolateProvider, $httpProvider) {
  8.     //allow django templates and angular to co-exist
  9.     $interpolateProvider.startSymbol('{[{');
  10.     $interpolateProvider.endSymbol('}]}');
  11. });
  12.  
  13. places.run(function($rootScope, $log, $http, $cookies) {
  14.     $http.defaults.headers.common['X-CSRFToken'] = $cookies['csrftoken'];
  15. });
  16.  
  17.  
  18. /*******************/
  19. /***** FACTORY *****/
  20. /*******************/
  21. places.factory('ModelUtils', function($http, $log) {
  22.  
  23.     var handleErrors =  function(serverResponse, status, errorDestination) {
  24.             if (angular.isDefined(errorDestination)) {
  25.                 if (status >= 500) {
  26.                     errorDestination.form = 'Server Error: ' + status;
  27.                 } else if (status >= 401) {
  28.                     errorDestination.form = 'Unauthorized Error: ' + status;
  29.                 } else {
  30.                     angular.forEach(serverResponse, function(value, key) {
  31.                         if (key != '__all__') {
  32.                             errorDestination[key] = angular.isArray(value) ? value.join("<br/>") : value;
  33.                         } else {
  34.                             errorDestination.form = errorDestination.form || '' + key + ':' + angular.isArray(value) ? value.join("<br/>") : value;
  35.                         }
  36.                     });
  37.                 }
  38.             }
  39.         };
  40.  
  41.     var ModelUtils = {
  42.         get: function(url, my_obj) {
  43.             $http.get(url, {params: {"limit": "0"}}).then(function(response){
  44.                 response_objects = response.data.objects;
  45.                 for (var i in response_objects){
  46.                     if(('/my/api/' + my_obj.sitio + '/') == response_objects[i].sitio){
  47.                         $http.delete(url + response_objects[i].idFav + '/');
  48.                     }
  49.                 }
  50.             });
  51.         },
  52.  
  53.         create: function(url, obj, errors) {
  54.             return $http.post(url,
  55.              {'sitio': '/my/api/' + obj.sitio + '/',
  56.               'usuario': '/my/api/' + obj.usuario + '/'}).
  57.                 success(function(response, status, headers, config) {
  58.                     obj = {};
  59.                     angular.extend(obj, response);
  60.                 }).
  61.                 error(function(response, status, headers, config) {
  62.                     handleErrors(response, status, errors);
  63.                 });
  64.  
  65.         },
  66.  
  67.         save: function(url, obj, errors) {
  68.             this.get(url, obj);
  69.             this.create(url, obj, errors);
  70.  
  71.         }
  72.     };
  73.     return ModelUtils;
  74. });
  75.  
  76.  
  77.  
  78. /***********************/
  79. /***** CONTROLLERS *****/
  80. /***********************/
  81.  
  82. places.controller('FavCtrl',
  83.     function FavCtrl($scope, $log, $http, ModelUtils) {
  84.     $scope.errors = {};
  85.  
  86.     $scope.saveItem = function() {
  87.         ModelUtils.save('/my/api/',
  88.             $scope.currentItem, $scope.errors);
  89.     };
  90.  
  91. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement