forked from Asana/Chrome-Extension-Example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_model.js
136 lines (125 loc) · 3.72 KB
/
server_model.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
* Library of functions for the "server" portion of an extension, which is
* loaded into the background and popup pages.
*
* Some of these functions are asynchronous, because they may have to talk
* to the Asana API to get results.
*/
Asana.ServerModel = {
_cached_user: null,
/**
* Called by the model whenever a request is made and error occurs.
* Override to handle in a context-appropriate way. Some requests may
* also take an `errback` parameter which will handle errors with
* that particular request.
*
* @param response {dict} Response from the server.
*/
onError: function(response) {},
/**
* Requests the user's preferences for the extension.
*
* @param callback {Function(options)} Callback on completion.
* workspaces {dict[]} See Asana.Options for details.
*/
options: function(callback) {
callback(Asana.Options.loadOptions());
},
/**
* Determine if the user is logged in.
*
* @param callback {Function(is_logged_in)} Called when request complete.
* is_logged_in {Boolean} True iff the user is logged in to Asana.
*/
isLoggedIn: function(callback) {
chrome.cookies.get({
url: Asana.ApiBridge.baseApiUrl(),
name: 'ticket'
}, function(cookie) {
callback(!!(cookie && cookie.value));
});
},
/**
* Get the URL of a task given some of its data.
*
* @param task {dict}
* @param callback {Function(url)}
*/
taskViewUrl: function(task, callback) {
// We don't know what pot to view it in so we just use the task ID
// and Asana will choose a suitable default.
var options = Asana.Options.loadOptions();
var pot_id = task.id;
var url = 'https://' + options.asana_host_port + '/0/' + pot_id + '/' + task.id;
callback(url);
},
/**
* Requests the set of workspaces the logged-in user is in.
*
* @param callback {Function(workspaces)} Callback on success.
* workspaces {dict[]}
*/
workspaces: function(callback, errback) {
var self = this;
Asana.ApiBridge.request("GET", "/workspaces", {},
function(response) {
self._makeCallback(response, callback, errback);
});
},
/**
* Requests the set of users in a workspace.
*
* @param callback {Function(users)} Callback on success.
* users {dict[]}
*/
users: function(workspace_id, callback) {
var self = this;
Asana.ApiBridge.request("GET", "/workspaces/" + workspace_id + "/users", {},
function(response) {
self._makeCallback(response, callback);
});
},
/**
* Requests the user record for the logged-in user.
*
* @param callback {Function(user)} Callback on success.
* user {dict[]}
*/
me: function(callback) {
var self = this;
if (self._cached_user !== null) {
callback(self._cached_user);
} else {
Asana.ApiBridge.request("GET", "/users/me", {},
function(response) {
if (!response.errors) {
self._cached_user = response.data;
}
self._makeCallback(response, callback);
});
}
},
/**
* Makes an Asana API request to add a task in the system.
*
* @param task {dict} Task fields.
* @param callback {Function(response)} Callback on success.
*/
createTask: function(workspace_id, task, callback, errback) {
var self = this;
Asana.ApiBridge.request(
"POST",
"/workspaces/" + workspace_id + "/tasks",
task,
function(response) {
self._makeCallback(response, callback, errback);
});
},
_makeCallback: function(response, callback, errback) {
if (response.errors) {
(errback || this.onError).call(null, response);
} else {
callback(response.data);
}
}
};