Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.

Commit ca7c865

Browse files
committed
Create TaskObject.js
1 parent a934690 commit ca7c865

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

modules/Task/TaskObject.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module = angular.module('App.Task');
2+
module.factory( 'Task', (BaseObject, $http) => {
3+
class Task extends BaseObject {
4+
static list(projectId) {
5+
return $http.get('/api/tasks', { params: { project_id: projectId } })
6+
.then( (response) => response.data.map( task => new Task(task) ) );
7+
}
8+
9+
10+
/**
11+
* task.create() - Creates a new task
12+
*
13+
* @note Demonstrates how to have the create wait until uploading is complete
14+
* Normally you might have to wait until the attachment is done before enabling
15+
* submission of the form, but this way allows the user to submit before uploading
16+
* is complete and makes it easy to show loading spinners.
17+
*
18+
* You could check either the `task.saving`, `task.creating`, or `task.uploading`
19+
* properties for truthy value when the user tries to navigate away from the page.
20+
*
21+
*/
22+
create() {
23+
// wraps `this.uploading` in a promise that resolves immediately if it is `null` or waits for the promise
24+
return this.creating = $q.when(this.uploading)
25+
.then( () => $http.post('/api/tasks', this) ) // uploading callback
26+
.then( response => return Object.assign(this, response.data) ) // creating callback
27+
.finally( () => this.creating = null ); // state cleanup (doesn't affect chaining)
28+
}
29+
30+
update() {
31+
// wraps `this.uploading` in a promise that resolves immediately if it is `null` or waits for the promise
32+
return this.updating = $q.when(this.uploading)
33+
.then( () => $http.post(`/api/tasks/${this.id}`, this) ) // uploading callback
34+
.then( response => return Object.assign(this, response.data) ) // creating callback
35+
.finally( () => this.updating = null ); // state cleanup (doesn't affect chaining)
36+
}
37+
38+
/**
39+
* task.upload() - Allow you to upload attachments to issues
40+
*
41+
* @note Added to demonstrate clean ways to have 1 method wait for another method to finish
42+
*/
43+
upload(attachment) {
44+
return this.uploading = $http.post(`/api/attachments`, attachment)
45+
.then( response => this.attachments = response.data )
46+
.finally( () => this.uploading = null ); // state cleanup (doesn't affect chaining)
47+
}
48+
}
49+
50+
return Task;
51+
});

0 commit comments

Comments
 (0)