This repository was archived by the owner on Sep 8, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathTaskObject.js
54 lines (49 loc) · 2.26 KB
/
TaskObject.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
module = angular.module('App.Task');
module.factory( 'Task', (BaseObject, $http) => {
class Task extends BaseObject {
static list(projectId) {
return $http.get('/api/tasks', { params: { project_id: projectId } })
.then( (response) => response.data.map( task => new Task(task) ) );
}
/**
* task.create() - Creates a new task
*
* @note Demonstrates how to have the create wait until uploading is complete
* Normally you might have to wait until the attachment is done before enabling
* submission of the form, but this way allows the user to submit before uploading
* is complete and makes it easy to show loading spinners.
*
* You could check either the `task.saving`, `task.creating`, or `task.uploading`
* properties for truthy value when the user tries to navigate away from the page.
*
*/
create() {
// wraps `this.uploading` in a promise that resolves immediately if it is `null` or waits for the promise
return this.cache('creating', () => $q.when(this.uploading)
.then( () => $http.post('/api/tasks', this) ) // uploading callback
.then( response => Object.assign(this, response.data) ) // creating callback
.finally( () => this.creating = null ) // state cleanup (doesn't affect chaining)
);
}
update() {
// wraps `this.uploading` in a promise that resolves immediately if it is `null` or waits for the promise
return this.cache('updating', () => $q.when(this.uploading)
.then( () => $http.post(`/api/tasks/${this.id}`, this) ) // uploading callback
.then( response => Object.assign(this, response.data) ) // creating callback
.finally( () => this.updating = null ) // state cleanup (doesn't affect chaining)
);
}
/**
* task.upload() - Allow you to upload attachments to issues
*
* @note Added to demonstrate clean ways to have 1 method wait for another method to finish
*/
upload(attachment) {
return this.cache('uploading', () => $http.post(`/api/attachments`, attachment)
.then( response => this.attachments = response.data )
.finally( () => this.uploading = null ) // state cleanup (doesn't affect chaining)
);
}
}
return Task;
});