forked from Apress/pro-angularjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListing 06.js
More file actions
58 lines (49 loc) · 1.56 KB
/
Listing 06.js
File metadata and controls
58 lines (49 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
angular.module("exampleApp", [])
.constant("baseUrl", "http://localhost:5500/products/")
.controller("defaultCtrl", function ($scope, $http, baseUrl) {
$scope.displayMode = "list";
$scope.currentProduct = null;
$scope.listProducts = function () {
$http.get(baseUrl).success(function (data) {
$scope.products = data;
});
}
$scope.deleteProduct = function (product) {
$http({
method: "DELETE",
url: baseUrl + product.id
}).success(function () {
$scope.products.splice($scope.products.indexOf(product), 1);
});
}
$scope.createProduct = function (product) {
$scope.products.push(product);
$scope.displayMode = "list";
}
$scope.updateProduct = function (product) {
for (var i = 0; i < $scope.products.length; i++) {
if ($scope.products[i].id == product.id) {
$scope.products[i] = product;
break;
}
}
$scope.displayMode = "list";
}
$scope.editOrCreateProduct = function (product) {
$scope.currentProduct =
product ? angular.copy(product) : {};
$scope.displayMode = "edit";
}
$scope.saveEdit = function (product) {
if (angular.isDefined(product.id)) {
$scope.updateProduct(product);
} else {
$scope.createProduct(product);
}
}
$scope.cancelEdit = function () {
$scope.currentProduct = {};
$scope.displayMode = "list";
}
$scope.listProducts();
});