Skip to content

Commit

Permalink
Make things DRY
Browse files Browse the repository at this point in the history
  • Loading branch information
paltman committed Jul 22, 2013
1 parent 4f556b7 commit 554a3cc
Showing 1 changed file with 26 additions and 50 deletions.
76 changes: 26 additions & 50 deletions js/eldarion-ajax-core.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,84 +39,60 @@

var Ajax = function () {};

Ajax.prototype.click = function (e) {
var $this = $(this),
url = $this.attr('href'),
method = $this.data('method');

if (!method) {
method = 'get';
}

$this.trigger('eldarion-ajax:begin', [$this]);

e.preventDefault();

Ajax.prototype._ajax = function ($el, url, method, data) {
$el.trigger('eldarion-ajax:begin', [$el]);
$.ajax({
url: url,
type: method,
dataType: 'json',
data: data,
headers: {'X-Eldarion-Ajax': true},
statusCode: {
200: function (data) {
if (!data) {
data = {};
200: function (responseData) {
if (!responseData) {
responseData = {};
}
$this.trigger('eldarion-ajax:success', [$this, data]);
$el.trigger('eldarion-ajax:success', [$el, responseData]);
},
500: function () {
$this.trigger('eldarion-ajax:error', [$this, 500]);
$el.trigger('eldarion-ajax:error', [$el, 500]);
},
400: function () {
$this.trigger('eldarion-ajax:error', [$this, 400]);
$el.trigger('eldarion-ajax:error', [$el, 400]);
},
404: function () {
$this.trigger('eldarion-ajax:error', [$this, 404]);
$el.trigger('eldarion-ajax:error', [$el, 404]);
}
},
complete: function (jqXHR, textStatus) {
$(document).trigger('eldarion-ajax:complete', [$this, jqXHR, textStatus]);
$(document).trigger('eldarion-ajax:complete', [$el, jqXHR, textStatus]);
}
});
};

Ajax.prototype.click = function (e) {
var $this = $(this),
url = $this.attr('href'),
method = $this.data('method');

if (!method) {
method = 'get';
}

e.preventDefault();

Ajax.prototype._ajax($this, url, method, null);
};

Ajax.prototype.submit = function (e) {
var $this = $(this),
url = $this.attr('action'),
method = $this.attr('method'),
data = $this.serialize();

$this.trigger('eldarion-ajax:begin', [$this]);

e.preventDefault();

$.ajax({
url: url,
type: method,
data: data,
dataType: 'json',
headers: {'X-Eldarion-Ajax': true},
statusCode: {
200: function (data) {
if (!data) {
data = {};
}
$this.trigger('eldarion-ajax:success', [$this, data]);
},
500: function () {
$this.trigger('eldarion-ajax:error', [$this, 500]);
},
400: function () {
$this.trigger('eldarion-ajax:error', [$this, 400]);
},
404: function () {
$this.trigger('eldarion-ajax:error', [$this, 404]);
}
},
complete: function (jqXHR, textStatus) {
$(document).trigger('eldarion-ajax:complete', [$this, jqXHR, textStatus]);
}
});
Ajax.prototype._ajax($this, url, method, data);
};

Ajax.prototype.cancel = function (e) {
Expand Down

0 comments on commit 554a3cc

Please sign in to comment.