senchalabs / jQTouch

Create powerful mobile apps with just HTML, CSS, and Zepto.js (or jQuery).
http://www.jqtouch.com/
MIT License
2.79k stars 592 forks source link

Internal showPageByHref() not calling callback functions (custom fix inside) #524

Open tegola opened 9 years ago

tegola commented 9 years ago

Internal showPageByHref function doesn't take into account the callback parameter:

For example, this callback found on jQT itself won't work:

showPageByHref(params.$el.attr("href"), {
  animation: animation,
  callback: function() {
    params.$el.removeClass("loading");
    return setTimeout(function() {
      return params.$el.removeClass('active');
    }, 250);
  },
  $referrer: params.$el
});

Here's the original showPageByHref with my fix applied (again, I'm not familiar with CoffeeScript, so can't do a pull request:

showPageByHref = (function(_this) {
  return function(href, options) {
    options = $.extend({}, {
      data: null,
      method: "GET",
      animation: null,
      $referrer: null
    }, options);
    if (href.charAt(0) !== '#') {
      return $.ajax({
        url: href,
        data: options.data,
        type: options.method,
        success: function(data) {
          var firstPage;
          firstPage = insertPages(data, options.animation);
          if (firstPage) {
            if (options.method === "GET" && _this.settings.cacheGetRequests === true && options.$referrer) {
              return options.$referrer.attr("href", "#" + firstPage.attr("id"));
            }
          }
          // FIX START         
          if (options.callback && typeof options.callback === "function") {
            options.callback.call();
          }
          // FIX END
        },
        error: function(data) {
          if (options.$referrer) {
            return options.$referrer.removeClass('active');
          }
        }
      });
    } else {
      if (options.$referrer) {
        return options.$referrer.removeClass('active');
      }
    }
  };
})(this);