-
Notifications
You must be signed in to change notification settings - Fork 0
/
restAngular:ngRoute.js
73 lines (61 loc) · 1.92 KB
/
restAngular:ngRoute.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
angular.module('project', ['restangular', 'ngRoute']).
config(function($routeProvider, RestangularProvider) {
$routeProvider.
when('/', {
controller:ListCtrl,
templateUrl:'list.html'
}).
when('/edit/:projectId', {
controller:EditCtrl,
templateUrl:'detail.html',
resolve: {
project: function(Restangular, $route){
return Restangular.one('projects', $route.current.params.projectId).get();
}
}
}).
when('/new', {controller:CreateCtrl, templateUrl:'detail.html'}).
otherwise({redirectTo:'/'});
RestangularProvider.setBaseUrl('https://api.mongolab.com/api/1/databases/angularjs/collections');
RestangularProvider.setDefaultRequestParams({ apiKey: '4f847ad3e4b08a2eed5f3b54' })
RestangularProvider.setRestangularFields({
id: '_id.$oid'
});
RestangularProvider.setRequestInterceptor(function(elem, operation, what) {
if (operation === 'put') {
elem._id = undefined;
return elem;
}
return elem;
})
});
function ListCtrl($scope, Restangular) {
Restangular.all("projects").getList().then(function(data){
console.log(data);
$scope.projects = data;
});
}
function CreateCtrl($scope, $location, Restangular) {
$scope.save = function() {
Restangular.all('projects').post($scope.project).then(function(project) {
$location.path('/list');
});
}
}
function EditCtrl($scope, $location, Restangular, project) {
var original = project;
$scope.project = Restangular.copy(original);
$scope.isClean = function() {
return angular.equals(original, $scope.project);
}
$scope.destroy = function() {
original.remove().then(function() {
$location.path('/list');
});
};
$scope.save = function() {
$scope.project.put().then(function() {
$location.path('/');
});
};
}