@@ -44,7 +44,7 @@ if (forcetk.Client === undefined) {
44
44
if ( window . $j === undefined ) {
45
45
$j = $ ;
46
46
}
47
-
47
+
48
48
/**
49
49
* The Client provides a convenient wrapper for the Force.com REST API,
50
50
* allowing JavaScript in Visualforce pages to use the API via the Ajax
@@ -68,37 +68,48 @@ if (forcetk.Client === undefined) {
68
68
} else {
69
69
this . instance_url = instanceUrl ;
70
70
}
71
- if ( typeof proxyUrl === 'undefined' || proxyUrl == null ) {
72
- this . proxy_url = location . protocol + "//" + location . hostname
73
- + "/services/proxy" ;
71
+ if ( typeof proxyUrl === 'undefined' || proxyUrl === null ) {
72
+ if ( location . protocol === 'file:' ) {
73
+ // In PhoneGap
74
+ this . proxy_url = null ;
75
+ } else {
76
+ // In Visualforce
77
+ this . proxy_url = location . protocol + "//" + location . hostname
78
+ + "/services/proxy" ;
79
+ }
74
80
} else {
81
+ // On a server outside VF
75
82
this . proxy_url = proxyUrl ;
76
83
}
77
84
}
78
85
79
86
/*
80
- * Low level utility function to call the Salesforce proxy endpoint.
87
+ * Low level utility function to call the Salesforce endpoint.
81
88
* @param path resource path relative to /services/data
82
89
* @param callback function to which response will be passed
90
+ * @param [error=null] function to which jqXHR will be passed in case of error
83
91
* @param [method="GET"] HTTP method for call
84
92
* @param [payload=null] payload for POST/PATCH etc
85
93
*/
86
- forcetk . Client . prototype . proxyAjax = function ( path , callback , method , payload ) {
94
+ forcetk . Client . prototype . ajax = function ( path , callback , error , method , payload ) {
87
95
var url = this . instance_url + '/services/data' + path ;
88
96
var sessionId = this . sessionId ;
89
97
90
98
$j . ajax ( {
91
99
type : ( typeof method === 'undefined' || method == null )
92
100
? "GET" : method ,
93
- url : this . proxy_url ,
101
+ url : ( this . proxy_url !== null ) ? this . proxy_url : url ,
94
102
contentType : 'application/json' ,
95
103
processData : false ,
96
104
data : ( typeof payload === 'undefined' || payload == null )
97
105
? null : payload ,
98
106
success : callback ,
107
+ error : error ,
99
108
dataType : "json" ,
100
109
beforeSend : function ( xhr ) {
101
- xhr . setRequestHeader ( 'SalesforceProxy-Endpoint' , url ) ;
110
+ if ( this . proxy_url !== null ) {
111
+ xhr . setRequestHeader ( 'SalesforceProxy-Endpoint' , url ) ;
112
+ }
102
113
xhr . setRequestHeader ( "Authorization" , "OAuth " + sessionId ) ;
103
114
}
104
115
} ) ;
@@ -109,48 +120,53 @@ if (forcetk.Client === undefined) {
109
120
* available, including the version, label, and a link to each version's
110
121
* root.
111
122
* @param callback function to which response will be passed
123
+ * @param [error=null] function to which jqXHR will be passed in case of error
112
124
*/
113
- forcetk . Client . prototype . versions = function ( callback ) {
114
- this . proxyAjax ( '/' , callback ) ;
125
+ forcetk . Client . prototype . versions = function ( callback , error ) {
126
+ this . ajax ( '/' , callback , error ) ;
115
127
}
116
128
117
129
/*
118
130
* Lists available resources for the client's API version, including
119
131
* resource name and URI.
120
132
* @param callback function to which response will be passed
133
+ * @param [error=null] function to which jqXHR will be passed in case of error
121
134
*/
122
- forcetk . Client . prototype . resources = function ( callback ) {
123
- this . proxyAjax ( '/' + this . apiVersion + '/' , callback ) ;
135
+ forcetk . Client . prototype . resources = function ( callback , error ) {
136
+ this . ajax ( '/' + this . apiVersion + '/' , callback , error ) ;
124
137
}
125
138
126
139
/*
127
140
* Lists the available objects and their metadata for your organization's
128
141
* data.
129
142
* @param callback function to which response will be passed
143
+ * @param [error=null] function to which jqXHR will be passed in case of error
130
144
*/
131
- forcetk . Client . prototype . describeGlobal = function ( callback ) {
132
- this . proxyAjax ( '/' + this . apiVersion + '/sobjects/' , callback ) ;
145
+ forcetk . Client . prototype . describeGlobal = function ( callback , error ) {
146
+ this . ajax ( '/' + this . apiVersion + '/sobjects/' , callback , error ) ;
133
147
}
134
148
135
149
/*
136
150
* Describes the individual metadata for the specified object.
137
151
* @param objtype object type; e.g. "Account"
138
152
* @param callback function to which response will be passed
153
+ * @param [error=null] function to which jqXHR will be passed in case of error
139
154
*/
140
- forcetk . Client . prototype . metadata = function ( objtype , callback ) {
141
- this . proxyAjax ( '/' + this . apiVersion + '/sobjects/' + objtype + '/'
142
- , callback ) ;
155
+ forcetk . Client . prototype . metadata = function ( objtype , callback , error ) {
156
+ this . ajax ( '/' + this . apiVersion + '/sobjects/' + objtype + '/'
157
+ , callback , error ) ;
143
158
}
144
159
145
160
/*
146
161
* Completely describes the individual metadata at all levels for the
147
162
* specified object.
148
163
* @param objtype object type; e.g. "Account"
149
164
* @param callback function to which response will be passed
165
+ * @param [error=null] function to which jqXHR will be passed in case of error
150
166
*/
151
- forcetk . Client . prototype . describe = function ( objtype , callback ) {
152
- this . proxyAjax ( '/' + this . apiVersion + '/sobjects/' + objtype
153
- + '/describe/' , callback ) ;
167
+ forcetk . Client . prototype . describe = function ( objtype , callback , error ) {
168
+ this . ajax ( '/' + this . apiVersion + '/sobjects/' + objtype
169
+ + '/describe/' , callback , error ) ;
154
170
}
155
171
156
172
/*
@@ -160,10 +176,11 @@ if (forcetk.Client === undefined) {
160
176
* the record, e.g. {:Name "salesforce.com", :TickerSymbol
161
177
* "CRM"}
162
178
* @param callback function to which response will be passed
179
+ * @param [error=null] function to which jqXHR will be passed in case of error
163
180
*/
164
- forcetk . Client . prototype . create = function ( objtype , fields , callback ) {
165
- this . proxyAjax ( '/' + this . apiVersion + '/sobjects/' + objtype + '/'
166
- , callback , "POST" , JSON . stringify ( fields ) ) ;
181
+ forcetk . Client . prototype . create = function ( objtype , fields , callback , error ) {
182
+ this . ajax ( '/' + this . apiVersion + '/sobjects/' + objtype + '/'
183
+ , callback , error , "POST" , JSON . stringify ( fields ) ) ;
167
184
}
168
185
169
186
/*
@@ -173,10 +190,11 @@ if (forcetk.Client === undefined) {
173
190
* @param fields comma-separated list of fields for which to return
174
191
* values; e.g. Name,Industry,TickerSymbol
175
192
* @param callback function to which response will be passed
193
+ * @param [error=null] function to which jqXHR will be passed in case of error
176
194
*/
177
- forcetk . Client . prototype . retrieve = function ( objtype , id , fieldlist , callback ) {
178
- this . proxyAjax ( '/' + this . apiVersion + '/sobjects/' + objtype + '/' + id
179
- + '?fields=' + fieldlist , callback ) ;
195
+ forcetk . Client . prototype . retrieve = function ( objtype , id , fieldlist , callback , error ) {
196
+ this . ajax ( '/' + this . apiVersion + '/sobjects/' + objtype + '/' + id
197
+ + '?fields=' + fieldlist , callback , error ) ;
180
198
}
181
199
182
200
/*
@@ -187,10 +205,11 @@ if (forcetk.Client === undefined) {
187
205
* the record, e.g. {:Name "salesforce.com", :TickerSymbol
188
206
* "CRM"}
189
207
* @param callback function to which response will be passed
208
+ * @param [error=null] function to which jqXHR will be passed in case of error
190
209
*/
191
- forcetk . Client . prototype . update = function ( objtype , id , fields , callback ) {
192
- this . proxyAjax ( '/' + this . apiVersion + '/sobjects/' + objtype + '/' + id
193
- , callback , "PATCH" , JSON . stringify ( fields ) ) ;
210
+ forcetk . Client . prototype . update = function ( objtype , id , fields , callback , error ) {
211
+ this . ajax ( '/' + this . apiVersion + '/sobjects/' + objtype + '/' + id
212
+ , callback , error , "PATCH" , JSON . stringify ( fields ) ) ;
194
213
}
195
214
196
215
/*
@@ -199,31 +218,34 @@ if (forcetk.Client === undefined) {
199
218
* @param objtype object type; e.g. "Account"
200
219
* @param id the record's object ID
201
220
* @param callback function to which response will be passed
221
+ * @param [error=null] function to which jqXHR will be passed in case of error
202
222
*/
203
- forcetk . Client . prototype . del = function ( objtype , id , callback ) {
204
- this . proxyAjax ( '/' + this . apiVersion + '/sobjects/' + objtype + '/' + id
205
- , callback , "DELETE" ) ;
223
+ forcetk . Client . prototype . del = function ( objtype , id , callback , error ) {
224
+ this . ajax ( '/' + this . apiVersion + '/sobjects/' + objtype + '/' + id
225
+ , callback , error , "DELETE" ) ;
206
226
}
207
227
208
228
/*
209
229
* Executes the specified SOQL query.
210
230
* @param soql a string containing the query to execute - e.g. "SELECT Id,
211
231
* Name from Account ORDER BY Name LIMIT 20"
212
232
* @param callback function to which response will be passed
233
+ * @param [error=null] function to which jqXHR will be passed in case of error
213
234
*/
214
- forcetk . Client . prototype . query = function ( soql , callback ) {
215
- this . proxyAjax ( '/' + this . apiVersion + '/query?q=' + escape ( soql )
216
- , callback ) ;
235
+ forcetk . Client . prototype . query = function ( soql , callback , error ) {
236
+ this . ajax ( '/' + this . apiVersion + '/query?q=' + escape ( soql )
237
+ , callback , error ) ;
217
238
}
218
239
219
240
/*
220
241
* Executes the specified SOSL search.
221
242
* @param sosl a string containing the search to execute - e.g. "FIND
222
243
* {needle}"
223
244
* @param callback function to which response will be passed
245
+ * @param [error=null] function to which jqXHR will be passed in case of error
224
246
*/
225
- forcetk . Client . prototype . search = function ( sosl , callback ) {
226
- this . proxyAjax ( '/' + this . apiVersion + '/search?s=' + escape ( sosl )
227
- , callback ) ;
247
+ forcetk . Client . prototype . search = function ( sosl , callback , error ) {
248
+ this . ajax ( '/' + this . apiVersion + '/search?s=' + escape ( sosl )
249
+ , callback , error ) ;
228
250
}
229
251
}
0 commit comments