Guest User

Untitled

a guest
Apr 12th, 2014
120
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) {
  43.             // recupera las entradas
  44.             var promise = $http.get(url, {params: {"limit": "0"}}).then(function(response){
  45.                 return response.data.objects;
  46.             });
  47.             return promise;
  48.         },
  49.      
  50.         create: function(url, obj, errors) {
  51.             // crea una entrada
  52.             return $http.post(url,
  53.              {'sitio': '/my/api/' + obj.sitio + '/',
  54.               'usuario': '/my/api/' + obj.usuario + '/'}).
  55.                 success(function(response, status, headers, config) {
  56.                     // limpiamos obj y le damos el valor del obj
  57.                     // que acabamos de anadir
  58.                     obj = {};
  59.                     angular.extend(obj, response);
  60.                 }).
  61.                 error(function(response, status, headers, config) {
  62.                     handleErrors(response, status, errors);
  63.                 });
  64.         },
  65.         del: function(url) {
  66.             // borra una entrada
  67.             return $http.delete(url);
  68.         },
  69.         save: function(url, obj, errors) {
  70.             // Anade el sitio a favoritos para el usuario.
  71.             // si el usuario ya habia anadido este sitio,
  72.             // lo borramos y lo creamos de nuevo.
  73.  
  74.             var promise = this.get(url);
  75.  
  76.             promise.then(function (asyncData) {
  77.                 for (var i in asyncData){
  78.                     // si para esta iteracion, sitio y usuario
  79.                     // coinciden con los que vamos a anadir, borramos la entrada.
  80.                     if((('/my/api/' + obj.sitio + '/') == asyncData[i].sitio) && (('/my/api/' + obj.usuario + '/') == asyncData[i].usuario)){
  81.                         formatted_url = (url + asyncData[i].idFav + '/')
  82.                         ModelUtils.del(formatted_url)
  83.                     }
  84.                 }
  85.                 // creamos el favorito
  86.                 ModelUtils.create(url,obj, errors);
  87.             });
  88.         }
  89.     };
  90.     return ModelUtils;
  91. });
  92.  
  93.  
  94.      
  95.      
  96.      
  97.     /***********************/
  98.     /***** CONTROLLERS *****/
  99.     /***********************/
  100.      
  101.     places.controller('FavCtrl',
  102.         function FavCtrl($scope, $log, $http, ModelUtils) {
  103.         $scope.errors = {};
  104.      
  105.         $scope.saveItem = function() {
  106.             ModelUtils.save('/my/api/',
  107.                 $scope.currentItem, $scope.errors);
  108.         };
  109.      
  110.     });
Advertisement
Add Comment
Please, Sign In to add comment