Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*******************/
- /***** GLOBAL ******/
- /*******************/
- var places = angular.module('places',['ngCookies']);
- places.config(function($interpolateProvider, $httpProvider) {
- //allow django templates and angular to co-exist
- $interpolateProvider.startSymbol('{[{');
- $interpolateProvider.endSymbol('}]}');
- });
- places.run(function($rootScope, $log, $http, $cookies) {
- $http.defaults.headers.common['X-CSRFToken'] = $cookies['csrftoken'];
- });
- /*******************/
- /***** FACTORY *****/
- /*******************/
- places.factory('ModelUtils', function($http, $log) {
- var handleErrors = function(serverResponse, status, errorDestination) {
- if (angular.isDefined(errorDestination)) {
- if (status >= 500) {
- errorDestination.form = 'Server Error: ' + status;
- } else if (status >= 401) {
- errorDestination.form = 'Unauthorized Error: ' + status;
- } else {
- angular.forEach(serverResponse, function(value, key) {
- if (key != '__all__') {
- errorDestination[key] = angular.isArray(value) ? value.join("<br/>") : value;
- } else {
- errorDestination.form = errorDestination.form || '' + key + ':' + angular.isArray(value) ? value.join("<br/>") : value;
- }
- });
- }
- }
- };
- var ModelUtils = {
- get: function(url, my_obj) {
- $http.get(url, {params: {"limit": "0"}}).then(function(response){
- response_objects = response.data.objects;
- for (var i in response_objects){
- if(('/my/api/' + my_obj.sitio + '/') == response_objects[i].sitio){
- $http.delete(url + response_objects[i].idFav + '/');
- }
- }
- });
- },
- create: function(url, obj, errors) {
- return $http.post(url,
- {'sitio': '/my/api/' + obj.sitio + '/',
- 'usuario': '/my/api/' + obj.usuario + '/'}).
- success(function(response, status, headers, config) {
- obj = {};
- angular.extend(obj, response);
- }).
- error(function(response, status, headers, config) {
- handleErrors(response, status, errors);
- });
- },
- save: function(url, obj, errors) {
- this.get(url, obj);
- this.create(url, obj, errors);
- }
- };
- return ModelUtils;
- });
- /***********************/
- /***** CONTROLLERS *****/
- /***********************/
- places.controller('FavCtrl',
- function FavCtrl($scope, $log, $http, ModelUtils) {
- $scope.errors = {};
- $scope.saveItem = function() {
- ModelUtils.save('/my/api/',
- $scope.currentItem, $scope.errors);
- };
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement