JQuery.when (deferred) or equivalent in ZenDesk app

In my ZenDesk app, I:

  • get some identifying information from the ticket and the requester
  • make multiple requests to another web service
  • change ticket with combined results

Using simple jQuery, you would coordinate this with jQuery.when (deferred) to invoke step 3 after all the queries in step 2 have completed:

$.when($.ajax("/page1"), $.ajax("/page2")) .done(function(page1result, page2result) { // modify the ticket with the results }); 
  • Is jQuery.when () available in the application? (I have not tried this.$.when() ).
  • If not, what is the preferred way to accomplish something like this? (Perhaps using Promises directly?)
+4
source share
1 answer

jQuery.when() is available through the application object as this.when() . Here is a simple example (version 0.5 framework) that creates a couple of trivial promises (using this.promise() , similar to jQuery.Deferred() ), then waits until they succeed / are allowed to call the third function.

Replace this.ajax(...) with this.createPromise() to do the real work.

app.js

 (function() { return { onActivate: function() { var promises = [] promises.push(this.createPromise().done(function() { console.log('promise 1 done'); })); promises.push(this.createPromise().done(function() { console.log('promise 2 done'); })); this.when.apply(this, promises).done(function() { console.log('all promises done'); }); }, // returns a promise that always succeeds after 1 sec. createPromise: function() { return this.promise(function(done, fail) { setTimeout(done, 1000); }); }, events: { 'app.activated': 'onActivate' } }; }()); 
+5
source

Source: https://habr.com/ru/post/1500768/


All Articles