8 |
11 |
12 | There are no items yet.
13 |
14 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
--------------------------------------------------------------------------------
/src/main/webapp/app/app.js:
--------------------------------------------------------------------------------
1 | (function(angular) {
2 | angular.module("myApp.controllers", []);
3 | angular.module("myApp.services", []);
4 | angular.module("myApp", ["ngResource", "spring-data-rest", "myApp.controllers", "myApp.services"]);
5 | }(angular));
--------------------------------------------------------------------------------
/src/main/webapp/app/controllers.js:
--------------------------------------------------------------------------------
1 | (function(angular) {
2 | var AppController = function($scope, Item) {
3 | Item.query(function(response) {
4 | $scope.items = response ? response : [];
5 | });
6 |
7 | $scope.addItem = function(description) {
8 | new Item({
9 | description: description,
10 | checked: false
11 | }).save(function(item) {
12 | $scope.items.push(item);
13 | });
14 | $scope.newItem = "";
15 | };
16 |
17 | $scope.updateItem = function(item) {
18 | item.save();
19 | };
20 |
21 | $scope.deleteItem = function(item) {
22 | item.remove(function() {
23 | $scope.items.splice($scope.items.indexOf(item), 1);
24 | });
25 | };
26 | };
27 |
28 | AppController.$inject = ['$scope', 'Item'];
29 | angular.module("myApp.controllers").controller("AppController", AppController);
30 | }(angular));
--------------------------------------------------------------------------------
/src/main/webapp/app/services.js:
--------------------------------------------------------------------------------
1 | (function(angular) {
2 | var HATEOAS_URL = './api/items';
3 | var ItemFactory = function($http, SpringDataRestAdapter) {
4 | function Item(item) {
5 |
6 | if (item._resources) {
7 | item.resources = item._resources("self", {}, {
8 | update: {
9 | method: 'PUT'
10 | }
11 | });
12 | item.save = function(callback) {
13 | item.resources.update(item, function() {
14 | callback && callback(item);
15 | });
16 | };
17 |
18 | item.remove = function(callback) {
19 | item.resources.remove(function() {
20 | callback && callback(item);
21 | });
22 | };
23 | } else {
24 | item.save = function(callback) {
25 | Item.resources.save(item, function(item, headers) {
26 | var deferred = $http.get(headers().location);
27 | return SpringDataRestAdapter.processWithPromise(deferred).then(function(newItem) {
28 | callback && callback(new Item(newItem));
29 | });
30 | });
31 | };
32 | }
33 |
34 | return item;
35 | }
36 |
37 | Item.query = function(callback) {
38 | var deferred = $http.get(HATEOAS_URL);
39 | return SpringDataRestAdapter.processWithPromise(deferred).then(function(data) {
40 | Item.resources = data._resources("self");
41 | callback && callback(_.map(data._embeddedItems, function(item) {
42 | return new Item(item);
43 | }));
44 | });
45 | };
46 |
47 | Item.resources = null;
48 |
49 | return Item;
50 | };
51 |
52 | ItemFactory.$inject = ['$http', 'SpringDataRestAdapter'];
53 | angular.module("myApp.services").factory("Item", ItemFactory);
54 | }(angular));
--------------------------------------------------------------------------------