<file_sep>jee-angular-todo

A very light-weight introductory project to Angular 1.x that utilizes Material Design.

Keywords: Angular, Angular 1.x, REST, Maven, Material

If you'd like to read more about this project, please see my blog: http://blog.danwatling.com<file_sep>angular.module('todo').controller('HomeController',
    function($scope, $log, TodoService) {
        $scope.data = [];

        $scope.refreshState = function() {
            TodoService.get().then(function(data) {
                $scope.data = data;
            });
        };

        $scope.doAddItem = function() {
            TodoService.add($scope.text).then(function() {
                $scope.refreshState();
                $scope.text = '';
            });
        };

        $scope.doUpdateItem = function(item) {
            TodoService.update(item.id, item.text).then(function() {
                $scope.refreshState();
            });
        };

        $scope.clickSave = function() {
            $scope.doAddItem();
        };

        $scope.clickUpdate = function(item) {
            $scope.doUpdateItem(item);
        };

        $scope.clickEdit = function(item) {
            item.isEditing = true;
        };

        $scope.clickDelete = function(item) {
            TodoService.remove(item.id).then(function() {
                $scope.refreshState();
            });
        };

        $scope.refreshState();
    }
);<file_sep>angular.module('todo').service('TodoService',
	function TodoService($http, $q) {
		var API_ROOT = '/todo/api/todo';
		this.get = function() {
			return $http({
				method: 'GET',
				url: API_ROOT
			}).then(function(response) {
			    return response.data;
			});
		};

		this.add = function(text) {
			return $http({
				method: 'POST',
				url: API_ROOT,
				data: text
			});
		};

		this.update = function(id, text) {
			return $http({
				method: 'PUT',
				url: API_ROOT + '/' + id,
				data: text
			});
		};

		this.remove = function(id) {
			return $http({
				method: 'DELETE',
				url: API_ROOT + '/' + id
			});
		};
	}
);