jquery-deferred

jQuery chaining and cascading then's and when's

寵の児 提交于 2019-12-09 04:27:31
I currently have a humongous if-then-else loop in front of me which first gets multiple cantinas from a webservice. Then it gets all available meals (for each menu) and all available side dishes (for each menu). For each of the meals as well as the sides it then checks for all additives and aggregates them. In the end I have multiple menus with meals, side dishes and all their additives. The following flowchart shows the process: I want to beautify the code and want to make use of promises with jQuery - but I can't figure out how to do it as I have to stack a then after a when (at least I

How to convert callback sample to deferred object?

徘徊边缘 提交于 2019-12-08 19:39:33
问题 I have a function that accepts a callback function where I pass the data back in. Can this converted to a deferred object for better practice? Here is what I got: var chapters; var getChapters = function (fnLoad) { //CACHE DATA IF APPLICABLE if (!chapters) { //CALL JSON DATA VIA AJAX $.getJSON('/chapters.txt') .done(function (json) { //STORE DATA IN LOCAL STORAGE chapters = Lawnchair(function () { this.save(json, function (data) { //CALL CALLBACK ON DATA fnLoad(data); }); }); }); } else { /

Jquery ajax done callback not responding to 201

馋奶兔 提交于 2019-12-08 16:37:36
问题 I have an ajax post as such: $.post("/api/v1/payment_methods/create_credit_card", values) .done (response) -> console.log("GOOD JOB") .fail (response) -> console.log("Adas") The response is a 201, however, done doesn't seem to be capturing it and instead it's going to fail. I thought 201 would of been considered a success and would of been captured by done. Any ideas of why it wouldn't be working? Note: The above code is in coffeescript, which doesn't really affect the question but explains

jQuery Deferred - Add callbacks to the Deferred contract at runtime

蓝咒 提交于 2019-12-08 06:49:26
问题 I need is to add an unknown number (known only at runtime) of .pipe() calls to the functions being monitored by jQuery's .when(). These calls will be based off of ajax calls made as a result of another asynchronous operation. Please see the code below for a more clear explanation: $.when( $.ajax({ async: true, success: function (data, textStatus, jqXhr) { console.log('Level 1') for(var i = 0; i < data.length; i++) jqXhr.pipe( $.ajax({ data: data[i], async: true, success: function (data,

jQuery chaining and cascading then's and when's

匆匆过客 提交于 2019-12-08 06:48:03
问题 I currently have a humongous if-then-else loop in front of me which first gets multiple cantinas from a webservice. Then it gets all available meals (for each menu) and all available side dishes (for each menu). For each of the meals as well as the sides it then checks for all additives and aggregates them. In the end I have multiple menus with meals, side dishes and all their additives. The following flowchart shows the process: I want to beautify the code and want to make use of promises

Using an array of deffered with jQuery.when()

微笑、不失礼 提交于 2019-12-08 06:22:32
问题 I'm using $.when to run 2 functions prior to some other logic. Now, in several cases I need to run a different set of functions prior to doing that same logic, so I wanted to pass an array of the functions to $.when , but couldn't make it run. Something like: function funcA(){ console.log("funcA"); } function funcB(){ console.log("funcB") } var funcArr = [funcA, funcB]; $.when(funcArr).then(function(){ console.log("DONE!"); }); But this doesn't work and the only thing written to the console

How do you attach callbacks to the deferred object that is attached to a deferred object?

夙愿已清 提交于 2019-12-08 03:40:00
问题 I have a chain of ajax requests that support a series of cascading drop down select lists. When you select a value in the 1st drop down, a request is fired to populate the 2nd, when that is complete (and the drop down filled), the next request fires to populate the 3rd drop down, and so on down the line. There are some variations to the way these request chains are formed, so I was hoping to assemble the requests using the jQuery Deferred objects. I see how I can chain the 2nd request to the

Declare jQuery AJAX call for execution later

℡╲_俬逩灬. 提交于 2019-12-08 02:39:28
问题 I could declare a jQuery AJAX call this way: var foo = $.ajax({ ... }); But that would actually perform the request right then and there, correct? How can I declare the AJAX call without executing it initially, then call it later? Something like: var foo = $.ajax({ ... }); // some stuff in between foo.execute(); Thanks. EDIT Little more info: What I really want to do is have a function that constructs an AJAX request based on parameters, returns it to the calling code, and have the calling

How are $.when, apply(), and $.done() interacting in this function?

ぃ、小莉子 提交于 2019-12-07 17:50:46
问题 A while ago, a SO user wrote this function for me to return a variable number of jQuery $.get() requests. The first part is pretty straightforward, but will someone elplain how $.when() , apply() , and $.done() are interacting. I don't understand in general, but very specifically how they they are accessing the deferred object that $.get() returns. function getHTML(qty_of_gets) { var dfdArr = [], i = 1; while (i <= qty_of_gets) { dfdArr.push($.get("get_test_" + i + ".php")); i++; } $.when

How to handle nested jquery deferred calls

本秂侑毒 提交于 2019-12-07 05:36:18
问题 I have a function to get some data, and the function should return a promise. In the function, I have to made 2 requests - one after another. I ended up with a nested deferrer call where the last call resolves on the deferrer the function will return. I'm new to this deferred stuff and wonder if this is the right solution. function getData(func) { var model = new Model(); var collection = new Collection(); var dfd = new jQuery.Deferred(); collection.fetch().then(function () { model.fetch()