Mercurial > bugzilla-dashboard
annotate js/bugzilla.js @ 106:aad1c0a17ba4
added JSON support for iphone.
author | Atul Varma <avarma@mozilla.com> |
---|---|
date | Thu, 29 Apr 2010 11:11:04 -0700 |
parents | d1f7c1d38abe |
children |
rev | line source |
---|---|
0 | 1 var Bugzilla = { |
2 BASE_URL: "https://api-dev.bugzilla.mozilla.org/latest", | |
3 BASE_UI_URL: "https://bugzilla.mozilla.org", | |
4 DEFAULT_OPTIONS: { | |
5 method: "GET" | |
6 }, | |
7 getShowBugURL: function Bugzilla_getShowBugURL(id) { | |
8 return this.BASE_UI_URL + "/show_bug.cgi?id=" + id; | |
9 }, | |
10 queryString: function Bugzilla_queryString(data) { | |
11 var parts = []; | |
12 for (name in data) { | |
13 var values = data[name]; | |
14 if (!values.forEach) | |
15 values = [values]; | |
16 values.forEach( | |
17 function(value) { | |
18 parts.push(encodeURI(name) + "=" + encodeURI(value)); | |
19 }); | |
20 } | |
21 return parts.join("&"); | |
22 }, | |
23 ajax: function Bugzilla_ajax(options) { | |
24 var newOptions = {__proto__: this.DEFAULT_OPTIONS}; | |
25 for (name in options) | |
26 newOptions[name] = options[name]; | |
27 options = newOptions; | |
28 | |
29 function onLoad() { | |
97
d1f7c1d38abe
success callback is no longer called if bugzilla error occurs on Bugzilla.ajax()
Atul Varma <avarma@mozilla.com>
parents:
72
diff
changeset
|
30 var response = JSON.parse(xhr.responseText); |
d1f7c1d38abe
success callback is no longer called if bugzilla error occurs on Bugzilla.ajax()
Atul Varma <avarma@mozilla.com>
parents:
72
diff
changeset
|
31 if (!response.error) |
d1f7c1d38abe
success callback is no longer called if bugzilla error occurs on Bugzilla.ajax()
Atul Varma <avarma@mozilla.com>
parents:
72
diff
changeset
|
32 options.success(response); |
d1f7c1d38abe
success callback is no longer called if bugzilla error occurs on Bugzilla.ajax()
Atul Varma <avarma@mozilla.com>
parents:
72
diff
changeset
|
33 // TODO: We should really call some kind of error callback |
d1f7c1d38abe
success callback is no longer called if bugzilla error occurs on Bugzilla.ajax()
Atul Varma <avarma@mozilla.com>
parents:
72
diff
changeset
|
34 // if this didn't work. |
0 | 35 } |
36 | |
69
51c1829956d9
Added mock XHR object and a simple bugzilla ajax test
Atul Varma <avarma@mozilla.com>
parents:
18
diff
changeset
|
37 var xhr = options.xhr ? options.xhr : new XMLHttpRequest(); |
0 | 38 var url = this.BASE_URL + options.url; |
39 | |
40 if (options.data) | |
41 url = url + "?" + this.queryString(options.data); | |
42 xhr.open(options.method, url); | |
43 xhr.setRequestHeader("Accept", "application/json"); | |
44 xhr.setRequestHeader("Content-Type", "application/json"); | |
45 xhr.addEventListener("load", onLoad, false); | |
46 xhr.send(null); | |
47 return xhr; | |
48 }, | |
49 getBug: function Bugzilla_getBug(id, cb) { | |
72
0eab9a3ff12f
replaced my last commit w/ a serial xhr queue, so that the dashboard requests are serialized but other ones aren't.
Atul Varma <avarma@mozilla.com>
parents:
69
diff
changeset
|
50 return this.ajax({url: "/bug/" + id, |
0eab9a3ff12f
replaced my last commit w/ a serial xhr queue, so that the dashboard requests are serialized but other ones aren't.
Atul Varma <avarma@mozilla.com>
parents:
69
diff
changeset
|
51 success: cb}); |
0 | 52 }, |
53 search: function Bugzilla_search(query, cb) { | |
72
0eab9a3ff12f
replaced my last commit w/ a serial xhr queue, so that the dashboard requests are serialized but other ones aren't.
Atul Varma <avarma@mozilla.com>
parents:
69
diff
changeset
|
54 return this.ajax({url: "/bug", |
0eab9a3ff12f
replaced my last commit w/ a serial xhr queue, so that the dashboard requests are serialized but other ones aren't.
Atul Varma <avarma@mozilla.com>
parents:
69
diff
changeset
|
55 data: query, |
0eab9a3ff12f
replaced my last commit w/ a serial xhr queue, so that the dashboard requests are serialized but other ones aren't.
Atul Varma <avarma@mozilla.com>
parents:
69
diff
changeset
|
56 success: cb}); |
0 | 57 } |
58 }; |