gazay/gon

Callback only runs on 200 response

etagwerker opened this issue · 3 comments

Hi,

I'm not sure if this is an issue, maybe it's a feature request.

gon is working fine and calling my callback when the response is 200 (OK) but it's not calling it when I get response different than 200.

The problem is that we are relying on done to call the callback which only works with successful responses: https://github.com/gazay/gon/blob/master/js/watch.js#L36

I'm wondering if we could have another callback (e.g. errorCallback)

What do you think?

Thanks

I'm proposing a solution like this:

  performAjax = function() {
    var xhr;
    xhr = $.ajax({
      type: options.type || 'GET',
      url: options.url,
      data: {
        _method: options.method,
        gon_return_variable: true,
        gon_watched_variable: name
      }
    });
    return xhr.done(callback).fail(errorCallback);
  };

And then:

gon.watch = function(name, possibleOptions, possibleCallback, errorCallback) {
  var callback, errorCallback, key, options, performAjax, timer, value, _base, _ref;
  if (typeof $ === "undefined" || $ === null) {
    return;
  }
  ...
gazay commented

Hello! Thank you for issue. Can you do a PR?

@gazay Sure, I can work on this.