r3916 - in trunk/pollen-ui-angular/src/main/webapp: . js js/controllers partials
Author: garandel Date: 2014-05-06 17:56:39 +0200 (Tue, 06 May 2014) New Revision: 3916 Url: http://forge.chorem.org/projects/pollen/repository/revisions/3916 Log: migrate restangular to resource / send a single json data Modified: trunk/pollen-ui-angular/src/main/webapp/index.html 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 trunk/pollen-ui-angular/src/main/webapp/partials/poll.html Modified: trunk/pollen-ui-angular/src/main/webapp/index.html =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/index.html 2014-05-06 08:56:15 UTC (rev 3915) +++ trunk/pollen-ui-angular/src/main/webapp/index.html 2014-05-06 15:56:39 UTC (rev 3916) @@ -30,10 +30,10 @@ <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular-route.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular-resource.min.js"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.11.0/ui-bootstrap-tpls.min.js"></script> - <script src="http://cdnjs.cloudflare.com/ajax/libs/restangular/1.4.0/restangular.min.js"></script> <script src="js/libs/ckeditor/ckeditor.js"></script> <script src="js/app.js"></script> + <script src="js/services.js"></script> <script src="js/controllers/pollCtrl.js"></script> </head> <body> Modified: trunk/pollen-ui-angular/src/main/webapp/js/app.js =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/js/app.js 2014-05-06 08:56:15 UTC (rev 3915) +++ trunk/pollen-ui-angular/src/main/webapp/js/app.js 2014-05-06 15:56:39 UTC (rev 3916) @@ -18,52 +18,8 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% */ -angular.module('pollen', ["restangular", 'ngRoute', 'pollControllers', 'ui.bootstrap'], function($httpProvider) { - // Use x-www-form-urlencoded Content-Type - $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8'; +angular.module('pollen', ['pollenServices', 'ngRoute', 'pollControllers', 'ui.bootstrap']) - /** - * The workhorse; converts an object to x-www-form-urlencoded serialization. - * @param {Object} obj - * @return {String} - */ - var param = function(obj) { - var query = '', name, value, fullSubName, subName, subValue, innerObj, i; - - for(name in obj) { - value = obj[name]; - - if(value instanceof Array) { - for(i=0; i<value.length; ++i) { - subValue = value[i]; - fullSubName = name + '[' + i + ']'; - innerObj = {}; - innerObj[fullSubName] = subValue; - query += param(innerObj) + '&'; - } - } - else if(value instanceof Object) { - for(subName in value) { - subValue = value[subName]; - fullSubName = name + '.' + subName ; - innerObj = {}; - innerObj[fullSubName] = subValue; - query += param(innerObj) + '&'; - } - } - else if(value !== undefined && value !== null) - query += encodeURIComponent(name) + '=' + encodeURIComponent(value) + '&'; - } - - return query.length ? query.substr(0, query.length - 1) : query; - }; - - // Override $http service's default transformRequest - $httpProvider.defaults.transformRequest = [function(data) { - return angular.isObject(data) && String(data) !== '[object File]' ? param(data) : data; - }]; -}) - .config(['$routeProvider', function($routeProvider) { $routeProvider.when('/', {templateUrl: './partials/home.html', controller: "HomeCtrl"}) .when('/poll/create', {templateUrl: './partials/poll.html', controller: "PollCreateCtrl"}) @@ -74,15 +30,7 @@ }]) -.config(['RestangularProvider', function (RestangularProvider) { - //RestangularProvider.setBaseUrl('http://demo.codelutin.com/pollen-rest-api-latest/v1'); - RestangularProvider.setBaseUrl('http://localhost:8080/pollen-rest-api/v1'); -}]) -.factory('PollsRest', ['Restangular', function(Restangular) { - return Restangular.service('polls'); -}]) - .directive('focusMe', function($timeout) { return { link: function($scope, element, attrs) { 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-06 08:56:15 UTC (rev 3915) +++ trunk/pollen-ui-angular/src/main/webapp/js/controllers/pollCtrl.js 2014-05-06 15:56:39 UTC (rev 3916) @@ -65,7 +65,7 @@ }]) -.controller('PollAdminCtrl', ['$scope', '$controller', '$modal', '$filter', '$timeout', 'Restangular', '$http', function ($scope, $controller, $modal, $filter, $timeout, Restangular, $http) { +.controller('PollAdminCtrl', ['$scope', '$controller', '$modal', '$filter', '$timeout', function ($scope, $controller, $modal, $filter, $timeout) { $controller('PollCtrl', {$scope:$scope}); var initChoice = function () { @@ -134,19 +134,6 @@ }); } - $scope.reset = function () { - $scope.poll = initPoll(); - $scope.voteChoice = $scope.poll.choice; - pollStorage.put({}); - } - - $scope.delete = function () { - $scope.poll = initPoll(); - $scope.voteChoice= $scope.poll.choice; - $scope.globalVariables.voted = false; - pollStorage.put({}); - } - var PollPopChoiceCtrl = function ($scope, $filter, $modalInstance, title, choice) { $scope.title = title; $scope.choice = choice; @@ -191,9 +178,11 @@ poll.endChoiceDate = poll.endDate; } } + /* else { poll.endChoiceDate = poll.endDate; } + */ }); if (!angular.isDefined($scope.poll.continuousResults)) { @@ -224,47 +213,41 @@ }]) -.controller('PollCreateCtrl', ['$scope', '$controller', 'Restangular', '$http', function ($scope, $controller, Restangular, $http) { +.controller('PollCreateCtrl', ['$scope', '$controller', 'SkeletonPoll', 'Poll', function ($scope, $controller, SkeletonPoll, NewPoll) { $controller('PollAdminCtrl', {$scope:$scope}); var initPoll = function () { - Restangular.service('polls').one('new').get().then(function (skeletonPoll) { - $scope.poll = skeletonPoll; + SkeletonPoll.get().$promise.then(function (poll) { + $scope.poll = poll; $scope.poll.choice = []; $scope.vote = {}; $scope.vote.choice = $scope.poll.choice; - }); + }) } initPoll(); $scope.save = function () { if ($scope.formValid) { - var data = {}; - data.poll = $scope.poll; - -// Restangular.all('polls').post('polls', data).then(function (data) {; - $http.post('http://localhost:8080/pollen-rest-api/v1/polls', data).success(function (data) { - $scope.test = data; - $scope.globalVariables.saved = true; - }); - + Poll.post($scope.poll); } else { $scope.globalVariables.errorForm = true; } + } + $scope.delete = function () { + $scope.poll = initPoll(); } }]) -.controller('PollEditCtrl', ['$scope', '$controller', '$routeParams', 'Restangular', '$http', function ($scope, $controller, $routeParams, Restangular, $http) { +.controller('PollEditCtrl', ['$scope', '$controller', '$routeParams', 'Poll', 'PollChoice', function ($scope, $controller, $routeParams, Poll, PollChoice) { $controller('PollAdminCtrl', {$scope:$scope}); var initPoll = function () { - - Restangular.one('polls', $routeParams.pollId).get().then(function (poll) { + Poll.get({pollId:$routeParams.pollId}).$promise.then(function (poll) { $scope.poll = poll; - Restangular.one('polls', $routeParams.pollId).all('choices').getList().then(function (choices) { + PollChoice.query({pollId:$routeParams.pollId}).$promise.then(function (choices) { $scope.poll.choice = choices; $scope.vote = {}; $scope.vote.choice = $scope.poll.choice; @@ -275,36 +258,37 @@ $scope.save = function () { if ($scope.formValid) { - var data = {}; - data.poll = $scope.poll; - -// Restangular.all('polls').post('polls', data).then(function (data) {; - $http.put('http://localhost:8080/pollen-rest-api/v1/polls/'+$routeParams.pollId, data).success(function (data) { - $scope.test = data; + Poll.save($scope.poll).$promise.then(function () { $scope.globalVariables.saved = true; }); - } else { $scope.globalVariables.errorForm = true; } } + + $scope.delete = function () { + $scope.$delete(); + $scope.poll = initPoll(); + $scope.globalVariables.voted = false; + } }]) -.controller('PollVoteCtrl', ['$scope', '$filter', '$controller', '$routeParams', 'Restangular', function ($scope, $filter, $controller, $routeParams, Restangular) { +.controller('PollVoteCtrl', ['$scope', '$filter', '$controller', '$routeParams', 'Poll', 'PollChoice', function ($scope, $filter, $controller, $routeParams, Poll, PollChoice) { $controller('PollCtrl', {$scope:$scope}); - Restangular.one('polls', $routeParams.pollId).get().then(function (poll) { + Poll.get({pollId:$routeParams.pollId}).$promise.then(function (poll) { $scope.poll = poll; - Restangular.one('polls', $routeParams.pollId).all('choices').getList().then(function (choices) { + PollChoice.query({pollId:$routeParams.pollId}).$promise.then(function (choices) { $scope.poll.choice = choices; - initVote(); - }) + $scope.vote = {}; + $scope.vote.choice = $scope.poll.choice; + }); }); $scope.globalVariables.editMode = false; - $scope.$watch('vote.ame', function (newVal) { + $scope.$watch('vote.name', function (newVal) { if (newVal != '') { $scope.globalVariables.errorForm = false; } @@ -346,12 +330,11 @@ } }]) -.controller('PollListCtrl', ['$scope', '$controller', 'Restangular', function ($scope, $controller, Restangular) { +.controller('PollListCtrl', ['$scope', '$controller', 'Poll', function ($scope, $controller, Poll) { $controller('PollCtrl', {$scope:$scope}); - Restangular.all('polls').getList().then(function (polls) { - $scope.polls = polls; - }); + $scope.polls = Poll.query(); + }]) ; \ No newline at end of file Modified: trunk/pollen-ui-angular/src/main/webapp/js/services.js =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/js/services.js 2014-05-06 08:56:15 UTC (rev 3915) +++ trunk/pollen-ui-angular/src/main/webapp/js/services.js 2014-05-06 15:56:39 UTC (rev 3916) @@ -18,7 +18,16 @@ * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% */ -angular.module('pollenServices', ['ngResource']).factory('Poll', function($resource) { - return $resource('http://localhost:8080/pollen/v1/polls/:pollId', {}, { - }); -}); \ No newline at end of file +angular.module('pollenServices', ['ngResource']) + +.factory('SkeletonPoll', ['$resource', function ($resource) { + return $resource('http://localhost:8080/pollen-rest-api/v1/polls/new', null, {}); +}]) + +.factory('Poll', ['$resource', function ($resource) { + return $resource('http://localhost:8080/pollen-rest-api/v1/polls/:pollId', {pollId : '@topiaId'}); +}]) + +.factory('PollChoice', ['$resource', function ($resource) { + return $resource('http://localhost:8080/pollen-rest-api/v1/polls/:pollId/choices/:choiceId', {choiceId : '@topiaId'}); +}]) \ No newline at end of file Modified: trunk/pollen-ui-angular/src/main/webapp/partials/poll.html =================================================================== --- trunk/pollen-ui-angular/src/main/webapp/partials/poll.html 2014-05-06 08:56:15 UTC (rev 3915) +++ trunk/pollen-ui-angular/src/main/webapp/partials/poll.html 2014-05-06 15:56:39 UTC (rev 3916) @@ -21,11 +21,12 @@ <form class="form-inline" name="pollForm" novalidate> - <alert type="'danger'" ng-if="globalVariables.errorForm"> Champ non remplie </alert> - <alert type="'success'" ng-if="globalVariables.editMode && globalVariables.saved"> Sondage sauvegardé..</alert> - <alert type="'success'" ng-if="!globalVariables.editMode && globalVariables.saved"> Vote effectué..</alert> + <alert type="danger" ng-if="globalVariables.errorForm"> Champ non remplie </alert> + <alert type="success" ng-if="globalVariables.editMode && globalVariables.saved"> Sondage sauvegardé..</alert> + <alert type="success" ng-if="!globalVariables.editMode && globalVariables.saved"> Vote effectué..</alert> - <alert type="'warning'" ng-if="globalVariables.voted"> Les votes ont commencé, certaine modification sont inaccessible.. </alert> + <alert type="warning" ng-if="globalVariables.voted"> Les votes ont commencé, certaine modification sont inaccessible.. </alert> + <alert type="warning" ng-if="globalVariables.created"> <a href="#/poll/edit/{{pollId}}"> Edition du sondage </a>.. </alert> <div class="pollTitle" edit-me="showEditTitle"> <h1 ng-hide="showEditTitle"> {{poll.title || 'Click Me for Editing'}} </h1> @@ -52,7 +53,6 @@ <hr/> <button type="button" class="btn btn-default" ng-model="globalVariables.editMode" btn-checkbox>Mode Edition</button> <button class="btn btn-primary" ng-click="save()">Save</button> - <button class="btn btn-warning" ng-click="reset();" ng-if="!globalVariables.voted">Reset</button> <button class="btn btn-danger" ng-click="delete();">Delete</button> <button class="btn btn-default" ng-click="popupSettings()">Settings</button> </div>
participants (1)
-
garandel@users.chorem.org