Author: garandel Date: 2014-05-07 15:14:48 +0200 (Wed, 07 May 2014) New Revision: 3919 Url: http://forge.chorem.org/projects/pollen/repository/revisions/3919 Log: fix create poll Modified: trunk/pollen-ui-angular/src/main/webapp/js/app.js trunk/pollen-ui-angular/src/main/webapp/js/controllers/pollCtrl.js trunk/pollen-ui-angular/src/main/webapp/js/services.js Modified: trunk/pollen-ui-angular/src/main/webapp/js/app.js =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/js/app.js 2014-05-07 09:21:47 UTC (rev 3918) +++ trunk/pollen-ui-angular/src/main/webapp/js/app.js 2014-05-07 13:14:48 UTC (rev 3919) @@ -18,8 +18,12 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% */ -angular.module('pollen', ['pollenServices', 'ngRoute', 'pollControllers', 'ui.bootstrap']) +angular.module('pollen', ['pollenServices', 'ngRoute', 'pollControllers', 'ui.bootstrap'], function($httpProvider) { + $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'; + $httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'; +}) + .config(['$routeProvider', function($routeProvider) { $routeProvider.when('/', {templateUrl: './partials/home.html', controller: "HomeCtrl"}) .when('/poll/create', {templateUrl: './partials/poll.html', controller: "PollCreateCtrl"}) Modified: trunk/pollen-ui-angular/src/main/webapp/js/controllers/pollCtrl.js =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/js/controllers/pollCtrl.js 2014-05-07 09:21:47 UTC (rev 3918) +++ trunk/pollen-ui-angular/src/main/webapp/js/controllers/pollCtrl.js 2014-05-07 13:14:48 UTC (rev 3919) @@ -213,11 +213,11 @@ }]) -.controller('PollCreateCtrl', ['$scope', '$controller', 'SkeletonPoll', 'Poll', function ($scope, $controller, SkeletonPoll, NewPoll) { +.controller('PollCreateCtrl', ['$scope', '$controller', 'Poll', '$http', function ($scope, $controller, Poll, $http) { $controller('PollAdminCtrl', {$scope:$scope}); var initPoll = function () { - SkeletonPoll.get().$promise.then(function (poll) { + Poll.get({cmd:'new'}).$promise.then(function (poll) { $scope.poll = poll; $scope.poll.choice = []; $scope.vote = {}; @@ -228,7 +228,7 @@ $scope.save = function () { if ($scope.formValid) { - Poll.post($scope.poll); + $scope.poll.$add(); } else { $scope.globalVariables.errorForm = true; @@ -241,7 +241,7 @@ }]) -.controller('PollEditCtrl', ['$scope', '$controller', '$routeParams', 'Poll', 'PollChoice', function ($scope, $controller, $routeParams, Poll, PollChoice) { +.controller('PollEditCtrl', ['$scope', '$controller', '$routeParams', '$location', 'Poll', 'PollChoice', function ($scope, $controller, $routeParams, $location, Poll, PollChoice) { $controller('PollAdminCtrl', {$scope:$scope}); var initPoll = function () { @@ -258,7 +258,7 @@ $scope.save = function () { if ($scope.formValid) { - Poll.save($scope.poll).$promise.then(function () { + $scope.poll.$update(function () { $scope.globalVariables.saved = true; }); } @@ -269,9 +269,8 @@ } $scope.delete = function () { - $scope.$delete(); - $scope.poll = initPoll(); - $scope.globalVariables.voted = false; + $scope.poll.$delete(); + $location.path('/'); } }]) Modified: trunk/pollen-ui-angular/src/main/webapp/js/services.js =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/js/services.js 2014-05-07 09:21:47 UTC (rev 3918) +++ trunk/pollen-ui-angular/src/main/webapp/js/services.js 2014-05-07 13:14:48 UTC (rev 3919) @@ -20,14 +20,36 @@ */ angular.module('pollenServices', ['ngResource']) -.factory('SkeletonPoll', ['$resource', function ($resource) { - return $resource('http://localhost:8080/pollen-rest-api/v1/polls/new', null, {}); -}]) +.factory('BaseUrl', function() { + return 'http://localhost\\:8080/pollen-rest-api/v1'; +}) -.factory('Poll', ['$resource', function ($resource) { - return $resource('http://localhost:8080/pollen-rest-api/v1/polls/:pollId', {pollId : '@topiaId'}); +.factory('Poll', ['$resource', 'BaseUrl', function ($resource, BaseUrl) { + var transformParam = function (data) { + return 'poll='+JSON.stringify(data); + }; + + return $resource(BaseUrl+'/polls/:pollId:cmd', {pollId:'@topiaId'}, { + 'add' : { + method : 'POST', + transformRequest : function (data, headersGetter) { + return transformParam(data); + } + }, + 'update' : { + method : 'PUT', + transformRequest : function (data, headersGetter) { + return transformParam(data); + } + } + }); }]) -.factory('PollChoice', ['$resource', function ($resource) { - return $resource('http://localhost:8080/pollen-rest-api/v1/polls/:pollId/choices/:choiceId', {choiceId : '@topiaId'}); +.factory('PollChoice', ['$resource', 'BaseUrl', function ($resource, BaseUrl) { + return $resource(BaseUrl+'/polls/:pollId/choices/:choiceId', {choiceId : '@topiaId'}, + { + 'add' : { method:'POST'}, + 'update' : {method:'PUT'} + } + ); }]) \ No newline at end of file