0

I am having an issue with $.ajax, $.when and apply. I have a constructor: The ajax request is not triggered when it should be : http://plnkr.co/edit/Ul9d8tB7BHoZyHzzQQyB?p=preview (see the console)

function updateGeneralTerm() {
  return {
    id: "GeneralCondition",
    ajax: function () {
      return $.ajax({
        type: 'POST',
        url: "@Url.Action("UpdateGeneralTerms", "Agreements")",
        data: $("#GeneralConditions").serialize()
      })
    }
  }
}

//I inject it in my custom function        
function CustomFunction(arr) {
  let arrOfAjax = arr.map(function (obj) {
    return obj.ajax
  });
  $.when.apply(null, arrOfAjax);
}

CustomFunction([new updateGeneralTerm()];

In my CustomFunction, I am checking other stuff, as does the form has changed... etc. However it doesn't seem to be relevant for my issue. Nothing happens.

In the future I might have n specific term that I'd like to update only if forms has changed.

My issue: ajax are not requested by $.when(). If I am changing to return obj.ajax(), the ajax request is triggered there directly not by the $.when(). I 'd like the $.when() to handle all ajax requests.

http://plnkr.co/edit/Ul9d8tB7BHoZyHzzQQyB?p=preview

Stefdelec
  • 2,711
  • 3
  • 33
  • 40
  • It's because you're using the reference of the `ajax()` function. You don't actually call it. Try using `return obj.ajax();`. It's a little odd the way you're proving the object returned from `updateGeneralTerm()` to `CustomFunction()` within an array. Presumably this is because in your production code you'll actually have a pre-populated array...? – Rory McCrossan Jan 30 '17 at 14:35
  • @RoryMcCrossan => not working. If I'm doing obj.ajax() , the $.ajax() is triggerred... I am sure because I commented $.when(...) and it reached the controller method. – Stefdelec Jan 30 '17 at 14:38
  • `If I'm doing obj.ajax() , the $.ajax() is triggerred` That's what I suggested, and what you're trying to achieve, is it not? Also please give more information than simply 'it doesn't work'. What doesn't work? Where is it failing? Have you checked for errors in the console, or the network tab to see the responseText? – Rory McCrossan Jan 30 '17 at 14:39
  • What I want is all "ajax" to be managed by $.when. If I am doing '''let arrOfAjax = arr.map(function (obj) { return obj.ajax() }); // $.when.apply(null, arrOfAjax);""" , It is reaching the controller method and it should not. (notice that I comment $.when(). (My "plugin" https://github.com/stefdelec/ajaxIfFormChanges/blob/master/index.js) – Stefdelec Jan 30 '17 at 14:43
  • works fine here with very minor adustments http://plnkr.co/edit/nA300vLazNqq1OydngoQ?p=preview – charlietfl Jan 30 '17 at 15:04
  • Tks. Look what I did in the $.when(). And it is still red.Your ajax request is triggered in the .map(), and not in the $.when() http://plnkr.co/edit/Ul9d8tB7BHoZyHzzQQyB?p=preview – Stefdelec Jan 30 '17 at 15:15

1 Answers1

1

try to rewrite your CustomFunction function to use spread operator:

function CustomFunction(arr) {
  let arrOfAjax = arr.map(function (obj) {
    return obj.ajax
  });
  $.when(...arrOfAjax).then(function(...results) {
    console.log(results);
  });
}
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
Andriy
  • 14,781
  • 4
  • 46
  • 50
  • Actually it is working. I messed with my mind ! So no need to check it. Thanks. However, it should work with ".apply", but I ll not go deeper in the problem. Thanks! – Stefdelec Jan 30 '17 at 19:06
  • `apply` was used in order to pass to `$.when` function any number of promises as an array, because `$.when` expects promises as separated by commas function arguments ($.when( d1, d2, d3 ).done(function ( v1, v2, v3 )...). `spread` operator allows us to pass argument as one array, but received by the function as separated by commas arguments, so no need in `apply` if we use `spread` (three dots) – Andriy Jan 30 '17 at 19:25
  • @Stefdelec please avoid editing your code into others answers. – Emile Bergeron Jan 31 '17 at 19:13