goatslacker / alt

Isomorphic flux implementation
http://alt.js.org/
3.45k stars 323 forks source link

Dispatch after ajax call #573

Closed tquetano-r7 closed 8 years ago

tquetano-r7 commented 8 years ago

Forgive the naivete, perhaps we've been doing it wrong so far, but the update to 0.18 has us a little befuddled on how to handle dispatches on success responses from AJAX calls.

A super-rudimentary version of our 0.17.9 code:

someAction() {
  $.ajax({
    url:"/some/url/location"
  })
  .success((data) => {
    this.dispatch(data);
  });
}

This was all honky-dory, only dispatched when the response came back. With the new 0.18 return-based methodology, this becomes quite difficult. "return data" inside of success would return not to the correct function, and wrapping either the dispatch in the success or the entire ajax call in an async setup does not work. While the documentation has been updated in some places, I cannot seem to find an example that has been updated for the new 0.18 setup.

My question is simple ... how does one handle this circumstance, i.e. dispatching following a successful AJAX call?

goatslacker commented 8 years ago
someAction() {
  return function (dispatch) {
    $.ajax({
      url:"/some/url/location"
    })
    .success((data) => {
      dispatch(data);
    });
  }  
}
tquetano-r7 commented 8 years ago

So this is easily the first thing that I tried, and it definitely dispatches ... but does nothing to prevent dispatch collision. Is there a way to use this method in a way that guarantees the one-dispatch-at-a-time requirement?

goatslacker commented 8 years ago

what do you mean by it does not prevent dispatch collision?