Skip to content

Commit

Permalink
make Util.maybeLoad take an object instead of just a URL, to give mor…
Browse files Browse the repository at this point in the history
…e control over how the xhr is done
  • Loading branch information
rbuels committed Mar 13, 2012
1 parent 3020195 commit 786d9d8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 24 deletions.
7 changes: 5 additions & 2 deletions js/Browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Browser.prototype.initialize = function() {
var brwsr = this;
if( typeof this.params.refSeqs == 'string' )
this.params.refSeqs = { url: this.params.refSeqs };
Util.maybeLoad(this.params.refSeqs.url, this.params.refSeqs,
Util.maybeLoad({ url: this.config.refSeqs.url, handleAs: 'json'}, this.config.refSeqs,
function(o) {
brwsr.addRefseqs(o);
});
Expand All @@ -121,7 +121,10 @@ Browser.prototype.loadConfig = function () {

(function(config) {
Util.maybeLoad(
config.url,
{
url: config.url,
handleAs: 'json'
},
config,
function(o) {
brwsr.addDeferred(
Expand Down
4 changes: 3 additions & 1 deletion js/NCList.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,12 @@ NCList.prototype.iterHelper = function(arr, from, to, fun, finish,
}
var chunk = this.lazyChunks[chunkNum];
finish.inc();
Util.maybeLoad(Util.resolveUrl(this.baseURL,
Util.maybeLoad({ url: Util.resolveUrl(this.baseURL,
this.lazyUrlTemplate.replace(
/\{Chunk\}/g, chunkNum
) ),
handleAs: 'json'
},
chunk,
(function (myChunkNum) {
return function(o) {
Expand Down
42 changes: 21 additions & 21 deletions js/Util.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,13 @@ Util.fillTemplate = function(template, fillWith) {

/**
* function to load a specified resource only once
* @param url URL to get
* @param stateObj object that stores the state of the load
* @param successCallback function to call on a successful load
* @param errorCallback function to call on an unsuccessful load
* @param {Object} dojoXhrArgs object containing arguments for dojo.xhrGet,
* like <code>url</code> and <code>handleAs</code>
* @param {Object} stateObj object that stores the state of the load
* @param {Function} successCallback function to call on a successful load
* @param {Function} errorCallback function to call on an unsuccessful load
*/
Util.maybeLoad = function (url, stateObj, successCallback, errorCallback) {
Util.maybeLoad = function ( dojoXhrArgs, stateObj, successCallback, errorCallback) {
if (stateObj.state) {
if ("loaded" == stateObj.state) {
successCallback(stateObj.data);
Expand All @@ -117,22 +118,21 @@ Util.maybeLoad = function (url, stateObj, successCallback, errorCallback) {
stateObj.state = "loading";
stateObj.successCallbacks = [successCallback];
stateObj.errorCallbacks = [errorCallback];
dojo.xhrGet(
{
url: url,
handleAs: "json",
load: function(o) {
stateObj.state = "loaded";
stateObj.data = o;
var cbs = stateObj.successCallbacks;
for (var c = 0; c < cbs.length; c++) cbs[c](o);
},
error: function() {
stateObj.state = "error";
var cbs = stateObj.errorCallbacks;
for (var c = 0; c < cbs.length; c++) cbs[c]();
}
});

var args = dojo.clone( dojoXhrArgs );
args.load = function(o) {
stateObj.state = "loaded";
stateObj.data = o;
var cbs = stateObj.successCallbacks;
for (var c = 0; c < cbs.length; c++) cbs[c](o);
};
args.error = function() {
stateObj.state = "error";
var cbs = stateObj.errorCallbacks;
for (var c = 0; c < cbs.length; c++) cbs[c]();
};

dojo.xhrGet( args );
}
};

Expand Down

0 comments on commit 786d9d8

Please sign in to comment.