-
Notifications
You must be signed in to change notification settings - Fork 31
/
jquery.parse.js
executable file
·197 lines (149 loc) · 5.34 KB
/
jquery.parse.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
(function($) {
var ns, _opts, methods, uriRgx;
//Plugin namespace you can change this if you want..
//i.e, ns = "db" = $.db.get/post/put/delete
ns = "parse";
//default opts
_opts = {
base: "https://api.parse.com/1/"
};
//public methods
methods = {};
//uriRgx
uriRgx = /(files|installations|login|push|roles|requestPasswordReset|users)/;
function _creds() {
var error;
if (_opts.app_id && _opts.rest_key) {
return true;
}
error = "Missing app_id, or rest_key authentication parameters.\n" +
"Pass these credentials to $." + ns + ".init\n" +
"app_id = Application Id\n" +
"rest_key = REST API Key";
alert(error);
$.error(error);
return false;
}
function _error(jqXHR, textStatus, errorThrown) {
$.error("$." + ns + " :" + textStatus + " " + errorThrown);
}
//TODO JSON.stringify dependency?
function _http(method, uri, data) {
var req;
if (!_creds()) {
return false;
}
req = {
//data
contentType: "application/json",
processData: false,
dataType: 'json',
//action
url: _opts.base + (uriRgx.test(uri) ? uri: "classes/" + uri),
type: method,
//Credentials
//NEW! Parse.com now supports CORS...https://parse.com/docs/rest
headers: {
"X-Parse-Application-Id": _opts.app_id,
"X-Parse-REST-API-Key": _opts.rest_key
}
};
//If a session token is present in $.parse.init, adds it to the header
if( _opts.session_token ) {
req.headers["X-Parse-Session-Token"] = _opts.session_token;
}
//if no data passed just return ajax
if (typeof data !== 'object') {
return $.ajax(req);
}
//makes for easier reuse of query objects passed in as reference
data = $.extend({}, data);
//if get request process data as application/x-www-form-urlencoded
if (method === 'GET') {
req.processData = true;
//if there is a where object it needs to be stringified first.
//no need to encodeURIComponent on data.where as $.ajax does that natively
if (data.where && typeof data.where === 'object') {
data.where = JSON.stringify(data.where);
}
}
//otherwise stringify all data.
else {
data = JSON.stringify(data);
}
//set request data
req.data = data;
return $.ajax(req);
}
function _response(req, cb, error) {
typeof cb === "function" && req.done(cb);
error = typeof error === 'function' ? error : _error;
req.fail(error);
return $[ns];
}
function _logger(method, uri, data){
var str = [ "$.", ns, ".", method, "(", "\"",uri,"\""];
data && str.push(", " + (JSON ? JSON.stringify(data) : "data") );
str = str.join('')+");";
$.publish && $.publish("parse.log", [str]);
return str;
}
//exports
methods.init = function(customOpts) {
$.extend(_opts, typeof customOpts === 'object' ? customOpts: {}, true);
return $[ns];
};
/*
Creates $.parse.get/post/put/delete methods
Examples....
$.parse.post('tasks',{ body : "Build all the things!" },function(json){
console.log(json);
});
*/
$.each(['GET', 'POST', 'PUT', 'DELETE'], function(i, action) {
var m = action.toLowerCase();
methods[m] = function() {
var args, uri, data, cb, req;
args = arguments;
uri = args[0];
data = args[1];
cb = args[2];
error = args[3]
if (typeof args[1] === 'function') {
data = false;
cb = args[1];
error = args[2];
}
_logger(m, uri, data);
req = _http(action, uri, data);
return _response(req, cb, error);
};
});
//alias methods
$.extend(methods, {
//@param Object data eg.. '{"username": "cooldude6", "password": "p_n7!-e8", "phone": "415-392-0202"}'
//@param Function optional callback
//@param Function optional error callback
//@return $[ns] aka $.parse
signup: function(data, cb, error) {
return this.post('users', data, cb, error);
},
//@param String username
//@param String password
//@param Function optional callback
//@param Function optional error callback
//@return $[ns] aka $.parse
login: function(username, password, cb, error) {
return this.get('login', { username: username, password: password }, cb, error);
},
//@param String email address of user
//@param Function optional callback
//@param Function optional error callback
//@return $[ns] aka $.parse
requestPasswordReset: function(email, cb, error) {
return this.post('requestPasswordReset', { email: email }, cb, error);
}
});
//attach methods to jQuery object using ns var aka 'parse'
$[ns] = methods;
})(jQuery);