| 1 |
define([ |
| 2 |
"dojo/_base/declare", |
| 3 |
"dojo/_base/kernel", |
| 4 |
"dojo/_base/xhr", |
| 5 |
"./model" |
| 6 |
], function (declare, kernel, xhrUtil, model){ |
| 7 |
|
| 8 |
return declare("dojox.atom.io.Connection",null,{ |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
constructor: function(sync, preventCache){ |
| 17 |
|
| 18 |
|
| 19 |
this.sync = sync; |
| 20 |
this.preventCache = preventCache; |
| 21 |
}, |
| 22 |
|
| 23 |
preventCache: false, |
| 24 |
|
| 25 |
alertsEnabled: false, |
| 26 |
|
| 27 |
getFeed: function(url, callback, errorCallback, scope){ |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
this._getXmlDoc(url, "feed", new model.Feed(), model._Constants.ATOM_NS, callback, errorCallback, scope); |
| 45 |
}, |
| 46 |
|
| 47 |
getService: function(url, callback, errorCallback, scope){ |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
this._getXmlDoc(url, "service", new model.Service(url), model._Constants.APP_NS, callback, errorCallback, scope); |
| 63 |
}, |
| 64 |
|
| 65 |
getEntry: function(url, callback, errorCallback, scope){ |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
this._getXmlDoc(url, "entry", new model.Entry(), model._Constants.ATOM_NS, callback, errorCallback, scope); |
| 81 |
}, |
| 82 |
|
| 83 |
_getXmlDoc: function(url, nodeName, newNode, namespace, callback, errorCallback, scope){ |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
if(!scope){ |
| 99 |
scope = kernel.global; |
| 100 |
} |
| 101 |
var ae = this.alertsEnabled; |
| 102 |
var xhrArgs = { |
| 103 |
url: url, |
| 104 |
handleAs: "xml", |
| 105 |
sync: this.sync, |
| 106 |
preventCache: this.preventCache, |
| 107 |
load: function(data, args){ |
| 108 |
var node = null; |
| 109 |
var evaldObj = data; |
| 110 |
var nodes; |
| 111 |
if(evaldObj){ |
| 112 |
|
| 113 |
if(typeof(evaldObj.getElementsByTagNameNS)!= "undefined"){ |
| 114 |
nodes = evaldObj.getElementsByTagNameNS(namespace,nodeName); |
| 115 |
if(nodes && nodes.length > 0){ |
| 116 |
node = nodes.item(0); |
| 117 |
}else if(evaldObj.lastChild){ |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
node = evaldObj.lastChild; |
| 122 |
} |
| 123 |
}else if(typeof(evaldObj.getElementsByTagName)!= "undefined"){ |
| 124 |
|
| 125 |
nodes = evaldObj.getElementsByTagName(nodeName); |
| 126 |
if(nodes && nodes.length > 0){ |
| 127 |
for(var i=0; i<nodes.length; i++){ |
| 128 |
if(nodes[i].namespaceURI == namespace){ |
| 129 |
node = nodes[i]; |
| 130 |
break; |
| 131 |
} |
| 132 |
} |
| 133 |
}else if(evaldObj.lastChild){ |
| 134 |
node = evaldObj.lastChild; |
| 135 |
} |
| 136 |
}else if(evaldObj.lastChild){ |
| 137 |
node = evaldObj.lastChild; |
| 138 |
}else{ |
| 139 |
callback.call(scope, null, null, args); |
| 140 |
return; |
| 141 |
} |
| 142 |
newNode.buildFromDom(node); |
| 143 |
if(callback){ |
| 144 |
callback.call(scope, newNode, evaldObj, args); |
| 145 |
}else if(ae){ |
| 146 |
throw new Error("The callback value does not exist."); |
| 147 |
} |
| 148 |
}else{ |
| 149 |
callback.call(scope, null, null, args); |
| 150 |
} |
| 151 |
} |
| 152 |
}; |
| 153 |
|
| 154 |
if(this.user && this.user !== null){ |
| 155 |
xhrArgs.user = this.user; |
| 156 |
} |
| 157 |
if(this.password && this.password !== null){ |
| 158 |
xhrArgs.password = this.password; |
| 159 |
} |
| 160 |
|
| 161 |
if(errorCallback){ |
| 162 |
xhrArgs.error = function(error, args){errorCallback.call(scope, error, args);}; |
| 163 |
}else{ |
| 164 |
xhrArgs.error = function(){ |
| 165 |
throw new Error("The URL requested cannot be accessed"); |
| 166 |
}; |
| 167 |
} |
| 168 |
xhrUtil.get(xhrArgs); |
| 169 |
}, |
| 170 |
|
| 171 |
updateEntry: function(entry, callback, errorCallback, retrieveUpdated, xmethod, scope){ |
| 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 |
|
| 198 |
if(!scope){ |
| 199 |
scope = kernel.global; |
| 200 |
} |
| 201 |
entry.updated = new Date(); |
| 202 |
var url = entry.getEditHref(); |
| 203 |
if(!url){ |
| 204 |
throw new Error("A URL has not been specified for editing this entry."); |
| 205 |
} |
| 206 |
|
| 207 |
var self = this; |
| 208 |
var ae = this.alertsEnabled; |
| 209 |
var xhrArgs = { |
| 210 |
url: url, |
| 211 |
handleAs: "text", |
| 212 |
contentType: "text/xml", |
| 213 |
sync: this.sync, |
| 214 |
preventCache: this.preventCache, |
| 215 |
load: function(data, args){ |
| 216 |
var location = null; |
| 217 |
if(retrieveUpdated){ |
| 218 |
location = args.xhr.getResponseHeader("Location"); |
| 219 |
if(!location){location = url;} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
var handleRetrieve = function(entry, dom, args){ |
| 224 |
if(callback){ |
| 225 |
callback.call(scope, entry, location, args); |
| 226 |
}else if(ae){ |
| 227 |
throw new Error("The callback value does not exist."); |
| 228 |
} |
| 229 |
}; |
| 230 |
self.getEntry(location,handleRetrieve); |
| 231 |
}else{ |
| 232 |
if(callback){ |
| 233 |
callback.call(scope, entry, args.xhr.getResponseHeader("Location"), args); |
| 234 |
}else if(ae){ |
| 235 |
throw new Error("The callback value does not exist."); |
| 236 |
} |
| 237 |
} |
| 238 |
return data; |
| 239 |
} |
| 240 |
}; |
| 241 |
|
| 242 |
if(this.user && this.user !== null){ |
| 243 |
xhrArgs.user = this.user; |
| 244 |
} |
| 245 |
if(this.password && this.password !== null){ |
| 246 |
xhrArgs.password = this.password; |
| 247 |
} |
| 248 |
|
| 249 |
if(errorCallback){ |
| 250 |
xhrArgs.error = function(error, args){errorCallback.call(scope, error, args);}; |
| 251 |
}else{ |
| 252 |
xhrArgs.error = function(){ |
| 253 |
throw new Error("The URL requested cannot be accessed"); |
| 254 |
}; |
| 255 |
} |
| 256 |
|
| 257 |
if(xmethod){ |
| 258 |
xhrArgs.postData = entry.toString(true); |
| 259 |
xhrArgs.headers = {"X-Method-Override": "PUT"}; |
| 260 |
xhrUtil.post(xhrArgs); |
| 261 |
}else{ |
| 262 |
xhrArgs.putData = entry.toString(true); |
| 263 |
var xhr = xhrUtil.put(xhrArgs); |
| 264 |
} |
| 265 |
}, |
| 266 |
|
| 267 |
addEntry: function(entry, url, callback, errorCallback, retrieveEntry, scope){ |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
if(!scope){ |
| 291 |
scope = kernel.global; |
| 292 |
} |
| 293 |
|
| 294 |
entry.published = new Date(); |
| 295 |
entry.updated = new Date(); |
| 296 |
|
| 297 |
var feedUrl = entry.feedUrl; |
| 298 |
var ae = this.alertsEnabled; |
| 299 |
|
| 300 |
|
| 301 |
if(!url && feedUrl){url = feedUrl;} |
| 302 |
if(!url){ |
| 303 |
if(ae){ |
| 304 |
throw new Error("The request cannot be processed because the URL parameter is missing."); |
| 305 |
} |
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
var self = this; |
| 310 |
var xhrArgs = { |
| 311 |
url: url, |
| 312 |
handleAs: "text", |
| 313 |
contentType: "text/xml", |
| 314 |
sync: this.sync, |
| 315 |
preventCache: this.preventCache, |
| 316 |
postData: entry.toString(true), |
| 317 |
load: function(data, args){ |
| 318 |
var location = args.xhr.getResponseHeader("Location"); |
| 319 |
if(!location){ |
| 320 |
location = url; |
| 321 |
} |
| 322 |
if(!args.retrieveEntry){ |
| 323 |
if(callback){ |
| 324 |
callback.call(scope, entry, location, args); |
| 325 |
}else if(ae){ |
| 326 |
throw new Error("The callback value does not exist."); |
| 327 |
} |
| 328 |
}else{ |
| 329 |
|
| 330 |
|
| 331 |
var handleRetrieve = function(entry, dom, args){ |
| 332 |
if(callback){ |
| 333 |
callback.call(scope, entry, location, args); |
| 334 |
}else if(ae){ |
| 335 |
throw new Error("The callback value does not exist."); |
| 336 |
} |
| 337 |
}; |
| 338 |
self.getEntry(location,handleRetrieve); |
| 339 |
} |
| 340 |
return data; |
| 341 |
} |
| 342 |
}; |
| 343 |
|
| 344 |
if(this.user && this.user !== null){ |
| 345 |
xhrArgs.user = this.user; |
| 346 |
} |
| 347 |
if(this.password && this.password !== null){ |
| 348 |
xhrArgs.password = this.password; |
| 349 |
} |
| 350 |
|
| 351 |
if(errorCallback){ |
| 352 |
xhrArgs.error = function(error, args){errorCallback.call(scope, error, args);}; |
| 353 |
}else{ |
| 354 |
xhrArgs.error = function(){ |
| 355 |
throw new Error("The URL requested cannot be accessed"); |
| 356 |
}; |
| 357 |
} |
| 358 |
xhrUtil.post(xhrArgs); |
| 359 |
}, |
| 360 |
|
| 361 |
deleteEntry: function(entry,callback,errorCallback,xmethod,scope){ |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
if(!scope){ |
| 378 |
scope = kernel.global; |
| 379 |
} |
| 380 |
|
| 381 |
var url = null; |
| 382 |
if(typeof(entry) == "string"){ |
| 383 |
url = entry; |
| 384 |
}else{ |
| 385 |
url = entry.getEditHref(); |
| 386 |
} |
| 387 |
if(!url){ |
| 388 |
callback.call(scope, false, null); |
| 389 |
throw new Error("The request cannot be processed because the URL parameter is missing."); |
| 390 |
} |
| 391 |
|
| 392 |
var xhrArgs = { |
| 393 |
url: url, |
| 394 |
handleAs: "text", |
| 395 |
sync: this.sync, |
| 396 |
preventCache: this.preventCache, |
| 397 |
load: function(data, args){ |
| 398 |
callback.call(scope, args); |
| 399 |
return data; |
| 400 |
} |
| 401 |
}; |
| 402 |
|
| 403 |
if(this.user && this.user !== null){ |
| 404 |
xhrArgs.user = this.user; |
| 405 |
} |
| 406 |
if(this.password && this.password !== null){ |
| 407 |
xhrArgs.password = this.password; |
| 408 |
} |
| 409 |
|
| 410 |
if(errorCallback){ |
| 411 |
xhrArgs.error = function(error, args){errorCallback.call(scope, error, args);}; |
| 412 |
}else{ |
| 413 |
xhrArgs.error = function(){ |
| 414 |
throw new Error("The URL requested cannot be accessed"); |
| 415 |
}; |
| 416 |
} |
| 417 |
if(xmethod){ |
| 418 |
xhrArgs.headers = {"X-Method-Override": "DELETE"}; |
| 419 |
dhxr.post(xhrArgs); |
| 420 |
}else{ |
| 421 |
xhrUtil.del(xhrArgs); |
| 422 |
} |
| 423 |
} |
| 424 |
}); |
| 425 |
}); |