Advertisement
jamesmyers

Untitled

Mar 29th, 2015
307
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* services.js */
  2. angular.module('pollService', [])
  3. .factory('Polls', function ($http) {
  4.  
  5.     return {
  6.         getAll: function () {
  7.             return $http.get('/api/polls');
  8.         },
  9.        
  10.         vote: function (pollID, responseID) {
  11.             return $http.post('api/polls/' + pollID + '/vote/', responseID);
  12.         },
  13.  
  14.         getPoll: function (id) {
  15.             return $http.get('/api/polls/' + id);
  16.         },
  17.  
  18.         newPoll: function () {
  19.             return $http.post('/api/polls/' + id, pollData);
  20.         }
  21.     }
  22. });
  23.  
  24. /* controllers.js */
  25.  
  26. //all polls controller
  27. pollGangster.controller('pollsController', function ($scope, $http, Polls) {
  28.     $scope.pageClass = 'page-home';
  29.     Polls.getAll()
  30.         .success(function (data) {
  31.             $scope.polls = data;
  32.         })
  33.         .error(function (data) {
  34.             console.log('Error: ' + data);
  35.         });
  36. })
  37.  
  38. //individual poll controller
  39. pollGangster.controller('pollController', function ($scope, $http, $routeParams, Polls) {
  40.     $scope.pageClass = 'page-poll';
  41.  
  42.     //submit vote
  43.     $scope.submitVote = function () {
  44.  
  45.         Polls.vote($scope.pollData.id, $scope.pollData.responses[$scope.index])
  46.         .success(function (data) {
  47.                 $scope.pollData = data;
  48.             })
  49.             .error(function (data) {
  50.                 console.dir(data);
  51.                 alert(data);
  52.             })
  53.     }
  54.  
  55.     Polls.getPoll($routeParams.id)
  56.         .success(function (data, etc) {
  57.             $scope.pollData = data;
  58.         })
  59.         .error(function (data) {
  60.             alert('Something went wrong...Are you trying something sneaky?');
  61.         })
  62. })
  63.  
  64.  
  65.  
  66. //new poll controller
  67. pollGangster.controller('newController', function ($scope, $http) {
  68.  
  69.     $scope.pageClass = 'page-new';
  70.  
  71.     $scope.pollData = {
  72.         responses: []
  73.     };
  74.  
  75.     $scope.addResponse = function () {
  76.         $scope.pollData.responses.push({
  77.             responseText: ''
  78.         });
  79.     }
  80.  
  81.     $scope.createPoll = function () {
  82.         $http.post('/api/polls', $scope.pollData);
  83.  
  84.     }
  85.  
  86. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement