Created
February 10, 2012 16:46
-
-
Save dtipson/1790763 to your computer and use it in GitHub Desktop.
bit of code (part of a larger object not shown) that handles batching of a huge CORS POST into several JSONP requests. The rwJSONP function returns a promise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| batchIt: function(q){ | |
| var b = $.Deferred(), | |
| batches = [], | |
| per=3, | |
| nq, | |
| nids=[]; | |
| $.each(q.ids,function(i,v){ | |
| nids.push(v); | |
| if (nids.length===per) { | |
| nq = $.extend({}, q); | |
| nq.ids = nids; | |
| batches.push(hashC.rwJSONP(nq)); | |
| nids = []; | |
| } | |
| }); | |
| $.when.apply(this,batches).then(function(){ | |
| //need to merge all the arguments back together into a single result, oy | |
| var result ={}; | |
| $.each(arguments,function(i,v){ | |
| $.extend(result,v[0]); | |
| }); | |
| b.resolve(result); | |
| },function(){ | |
| //one of the batches failed. darn | |
| b.reject(); | |
| }); | |
| return b.promise(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment