jquery-deferred

How to properly utilize jQuery Deferred/promise on auto-invoked function such as .ajax()'s beforeSend()?

扶醉桌前 提交于 2019-12-13 07:17:54
问题 I'm working on extending jQuery's ajaxPrefilter interface to add additional functionality to AJAX calls; namely tacking header data onto the XHR request using setRequestHeader() within the beforeSend() method. The thing is, both the ajaxPrefilter and ajax call itself could contain beforeSend options, and one or both of them could contain async functionality. Assume my ajaxPrefilter looks as such: (function($) { $.ajaxPrefilter(function(options, originalOptions, jqXHR) { var auth = $.cookie(

Using deferred with jquery ajax calls and async processes along with user confirmation reading in between

浪尽此生 提交于 2019-12-13 03:20:45
问题 Background This is in continuation to my question Correct way to set up a sequence of synchronous/asynchronous functions all of which can stop form submission and further processing? I got the answer on what was the correct way (deferred pipe), but I am still not able to actually implement this. I just started reading on jquery deferred API today and I could not grasp much yet. The jQuery API documentations seem too complicated with little example. Could anyone put links to some basic demos

Using jQuery deferred and promise inside loop

為{幸葍}努か 提交于 2019-12-13 03:10:11
问题 This is what i am trying to achieve, I have a sorted array which i pass to jQuery each. Inside each there is an ajax call which will fetch me the desired data and every time push into another array(lets call it allJsonData). Finally i display allJsonData. Problem is whenever i display allJsonData, elements are always displayed inconsistently (not alphabetically/random order). I am expecting allJsonData to be displayed alphabetically (that is AList data first, BList data second, CList data

How can I write this as a jQuery deffered function

别等时光非礼了梦想. 提交于 2019-12-13 00:11:05
问题 I need to get ALL the tables in the database and then I delete them one by one. However each DROP TABLE is asnyc. Most importantly the returned promise should only be resolved when all the tables have been dropped. clear: function() { var dfd = $.Deferred(); var sql = "SELECT name FROM sqlite_master WHERE type='table' AND name != ?", args = ["__WebKitDatabaseInfoTable__"]; var dbTableNamesResult = function(tx, result) { var dropSql = ""; for (var i = 0; i < result.rows.length; i++) { dropSql

Using deferred to chain loops with multiple ajax calls

回眸只為那壹抹淺笑 提交于 2019-12-12 14:22:57
问题 There are multiple questions that already have an answer about this, but all aren't working so far it this kind of setup. function login(u,p) { console.log(1); return $.post(url, {u,p}); } function out() { console.log(3); //a function that does not return deferred // clear cookies } function doSomething() { console.log(2); // a function that returns a deferred return $.post(...); } var data = [{u: 'au', p: 'ap'}, {u: 'bu', p: 'bp'}] $.each(data, function(k,v){ login(v.u, v.p).then(doSomething

Wait until promise and nested thens are complete

别说谁变了你拦得住时间么 提交于 2019-12-12 10:38:34
问题 I'm returning a promise from a function like this: resultPromise = dgps.utils.save(opportunity, '/api/Opportunity/Save', opportunity.dirtyFlag).then(function () { self.checklist.saveChecklist(opportunity).then(function () { self.competitor.save(opportunity.selectedCompetitor()).then(function ... etc. return resultPromise; Let's say the above function is called save. In the calling function I want to do wait for the entire chain to complete and then do something. My code there looks like this:

Does this JavaScript function need to return a Promise?

好久不见. 提交于 2019-12-12 10:05:35
问题 Let's say I have some JS that makes an AJAX call like this: $.getJSON(myUrl, { targetState: state }, function (jsonData) { }).success(function (jsonData) { ... ... ... }); Now let's assume I want to wrap this code in a function and have it return some value, in the success block, so I can call it from various places in my app. Should the function I create return a Promise? I'm thinking it probably does, but I've never created a JS function that returned a Promise so I'm not entirely certain

Dealing with Arrays of Deferred Objects

拟墨画扇 提交于 2019-12-12 07:35:55
问题 Since using $.Deferred I've run into this scenario a couple times: I have a list of values each of which yields a Deferred Object in some way and I want to execute a callback once all of the Deferred Objects are resolved. A more concrete example would be something like this: var urls = [ 'foo.com', 'bar.com', 'baz.com', 'qux.com' ], defers = [], defer; for( var i = 0, j = urls.length; i < j; i++ ){ defer = $.ajax({ url: 'http://' + urls[ i ] }); defers.push(defer); } $.when.apply(window,

Wait until all jquery ajax request are done? (part 2)

隐身守侯 提交于 2019-12-12 06:45:13
问题 I checked this post and the answer is really good: Wait until all jQuery Ajax requests are done? But I just want to be more generic: I wonder how we can use this logic for a List of services (ajaxservices) and list of callbacks e.g. ajaxservices = ["url-getdata1", "url-getdata2"]; callbacks = [callbackdata1, callbackdata2]; callbackdata1 = function (data){...} $.when(/*somehow call all ajaxservices[]*/).done(function (dataList) { for (var i = 0; i < callbacks.length; i++) { callbacks[i]

Geocoding using Google Maps API v3 - Linking the original request to the response

♀尐吖头ヾ 提交于 2019-12-12 06:35:49
问题 I have a list of schools that I want to plot on a Google Map. I'm using Google's Geocoding Service to lookup the lng/lat for a given postcode, upon successfully retrieving this information I want to drop a marker, together with adding the appropriate event listener that opens an infobox when a given marker is clicked. When I make a request to the geocoder it's in the context of a school, when I receive a callback I lose this context. You'll see from code below that I've come up with a clunky