jquery-deferred

Ajax API calls in loop need to be executed in order

≡放荡痞女 提交于 2019-12-04 23:16:03
lets say you have a scenario where you needed to create a .csv output in a page's textarea... So I have an array of queries which i loop. Inside the loop im passing a query to an ajax call... I need to append the result of the ajax call into the textarea. My question is how do you get the results printed out in the order they are requested (basically the order in the queries array) //example array to loop. var queries= ['query1', 'query', 'query3', 'query4']; //the textarea where im going to print the results in order later to open in excel as a .csv file var $csvText= $('#some-text-area'); /

Backbone, trigger an events when all is loaded

微笑、不失礼 提交于 2019-12-04 17:11:23
In my backbone app I load 3 Collections and I bind the "reset" event at the render function. So, in this way, when I fetch the Collections, I print the various results, but not simultaneously. I would use the jquery deferred methods ($.when, $.then) to print all simultaneously, how to do this if I use the "bind events" on the views? this is the code: Router: App.Routers.test1 = Backbone.Router.extend({ routes: { "/test" : "test" }, initialize: function() { // Models this.Models_1 = new App.Collections.coll_1; this.Models_2 = new App.Collections.coll_2; this.Models_3 = new App.Collections.coll

Can jQuery deferreds be cancelled?

给你一囗甜甜゛ 提交于 2019-12-04 14:59:39
问题 I have a situation where I want to cancel a deferred. The deferred is associated with an ajax call. Why I am using deferreds I don't use the normal xhr objects returned by $.ajax. I'm using jsonp, which means I can't use HTTP status codes for error handling and have to embed them in the responses. The codes are then examined and an associated deferred object is marked as resolved or rejected accordingly. I have a custom api function that does this for me. function api(options) { var url =

Chaining multiple ajax calls with jquery deferred objects

雨燕双飞 提交于 2019-12-04 14:47:43
问题 While looking for a solution similar to as described here: How to chain ajax calls using jquery I am looking for a solution using jquery v1.52. I have a set of ajax requests to be made. But each ajax request is to be sent only after completing the previous ajax call. I am trying to achieve this with jquery 1.5.2 but just unable to. Here is what I modified from the above mentioned example. It does not work. Can anyone help me get this working? Expected output is http://jsfiddle.net/k8aUj/3/ P

jQuery Promise then not working after AJAX

送分小仙女□ 提交于 2019-12-04 12:52:00
问题 I have my Promise defined as so: myFunc = function() { $.getJSON("./rest/api/some/url", function(json, textStatus) { console.log("AJAX call hit!"); }); }; $.when(myFunc()).then(function() { console.log("Then block hit!"); }); and in console it's being output as: Then block hit! AJAX call hit! I need the AJAX call hit! first followed by the Then block hit! . Any idea why this is happening? I even tried to implement a custom callback function (a standard example I found on Stackoverflow) and it

Nested jQuery $.when

雨燕双飞 提交于 2019-12-04 11:52:09
问题 Essentially I am tring to write this: var async1 = $.when( a1() ).then(function(){ a2() }); var async2 = $.when( a3() ).then(function(){ a4() }); $.when(async1, async2).then(function(){ console.log("complete"); }); But at the moment when a1 and a3 have executed the function considers itself resolved. I put together the same example in a fiddle: http://jsfiddle.net/Z7fzR/ 回答1: You never actually return the promise objects created by a2() and a4() from the callback; this effectively returns

jQuery Waiting for Multiple Events to fire

泄露秘密 提交于 2019-12-04 10:21:13
问题 Is it possible to "wait" for multiple events before executing a function? I know its possible with jQuery deferred obejcts to use $.when to wait for two promises to resolve but I wondered if its possible with just plain old events? Lets say a click event and a custom event trigger an alert, for example: $.when("click", "customEvent").then(function(){ alert("yay"); }); 回答1: You could use deferred, you just have to create them yourself. var clickDfd = $.Deferred(); var eventDfd = $.Deferred();

How do you use .when().then() to trigger a function when using deffered objects in the .when()?

我们两清 提交于 2019-12-04 09:21:37
I have a page in which I have a variable number of AJAX calls to make which are triggered on a common event. The AJAX calls themselves are to update related fields in a SQL database. Once all the calls are complete, I want to refresh the page so that it now reflects the changes just made. I used the following question/answer on here to get so far.. jQuery when each is completed, trigger function Here is my code so far: var team_update = function(e) { var $items = $('.sortable-items'); var XHRs = []; $items.each(function(){ var team_id = $( this ).attr("id"); var data = { tid: team_id, users: $

Find first available data source with jQuery Deferred

拜拜、爱过 提交于 2019-12-04 07:44:24
So I was asked this at an interview, but it brought up a good use case. Assume that you have a bunch of data sources. You want to find the first available one and process it and ignore the rest. So something like: var datasources = new Array("somedatabase1/pizza","somedatabase2/beer","somedatabase3/llama"); var dfds = new Array(); $.each(datasources,function(source){ dfds.push($.getJSON(source)); }); $.when(dfds).done(function(){alert("they are all done");}); Ignore that I really don't think when accepts an array (maybe it does). This of course would make it wait till they are all completed. I

Provide a default 'fail' method for a jQuery deferred object

依然范特西╮ 提交于 2019-12-04 05:32:00
I am writing a Javascript API client using jQuery. My top level request method looks like this: function request(method, uri, params, proxies) { var deferred = $.Deferred(); $.ajax({ data: method == 'GET' ? params : JSON.stringify(params), contentType: 'application/json', dataType: 'json', url: api.root + uri, type: method, xhrFields: { withCredentials: true } }).done(function(body) { deferred.resolveWith(this, [body.data]); }).fail(function(xhr) { deferred.rejectWith(this, [xhr]); }); return deferred.promise(); }, How can I have a default fail handler for my returned deferred? That is, if the