forked from angular-ui/AngularJS-StyleGuide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProject.js
119 lines (109 loc) · 3.32 KB
/
Project.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
Project Module
============
*/
var module = angular.module('App.Project', ['ui.router', 'ui.bootstrap'])
module.config( ($stateProvider) => {
$stateProvider.state( 'projects', {
parent: 'authenticated',
url: '/projects',
templateUrl: 'modules/Project/Projects.html',
controller: 'Projects',
resolve: {
projects: ($http, authenticatedUser, Project) => {
return $http.get('/api/projects', { params: { user_id: authenticatedUser.id } }).then( (response) => {
return response.data.map( (project) => {
return new Project(project);
});
});
}
},
// `breadcrumbs` resolved in `authenticated` state
onEnter: function(breadcrumbs) {
breadcrumbs.push({ label: 'Projects', sref: 'projects' });
},
onExit: function(breadcrumbs) {
breadcrumbs.pop();
},
});
$stateProvider.state( 'projects.new', {
url: '/new', // /projects/new (state must be defined BEFORE /:projectId)
resolve: {
project: (authenticatedUser, Project) => {
return new Project({ user_id: authenticatedUser.id });
}
},
templateUrl: 'modules/Project/Form.html',
controller: 'ProjectForm',
// `breadcrumbs` resolved in `authenticated` state
onEnter: function(breadcrumbs) {
breadcrumbs.push({ label: 'New', sref: 'projects.new' });
},
onExit: function(breadcrumbs) {
breadcrumbs.pop();
}
});
$stateProvider.state( 'project', {
parent: 'projects',
url: '/:projectId', // /projects/:projectId (state must be defined AFTER /new)
views: {
'': { // Projects.html: <ui-view></ui-view>
templateUrl: 'modules/Project/Project.html',
controller: 'Project'
},
'header@authenticated': { // Authenticated.html: <ui-view name="header"></ui-view>
templateUrl: 'modules/Project/ProjectHeader.html',
controller: 'ProjectHeader'
}
},
resolve: {
project: ($http, $stateParams, Project) => {
return $http.get('/api/projects/' + $stateParams.id).then( (response) => {
return new Project(response.data);
});
}
},
onEnter: (project, breadcrumbs) => {
project.open();
breadcrumbs.push({ label: project.name, sref: 'project' }); // Params inferred when going up
},
onExit: (project, breadcrumbs) => {
project.close();
breadcrumbs.pop();
}
});
$stateProvider.state( 'project.edit', {
templateUrl: 'modules/Project/Form.html',
controller: 'ProjectForm'
});
});
module.controller( 'Projects', ($scope, projects) => {
$scope.projects = projects;
});
module.controller( 'Project', ($scope, project) => {
$scope.project = project;
});
module.controller( 'ProjectHeader', ($scope, project) => {
$scope.project = project;
});
module.controller( 'ProjectForm', ($scope, project) => {
// injected `project` is either a new object or an existing object
$scope.project = project;
});
module.factory('ProjectObject', (BaseObject, $http) => {
class Project extends BaseObject {
create() {
return $http.post('/api/projects', this ).then( (response) => {
this.id = response.data.id;
return response.data;
});
}
update() {
return $http.put('/api/projects/' + this.id, this );
}
close() {
super.close();
}
}
return ProjectObject;
});