Last active
December 12, 2015 02:57
-
-
Save bruno-barros/ef7de72372584757c061 to your computer and use it in GitHub Desktop.
jQuery plugin to send ajax request using library Ajax (https://gist.github.com/bruno-barros/98db76c8d777caa3c414)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* WelAjaxForm | |
* | |
* Example: | |
* $('.news-signin').WelAjaxForm({ | |
action: 'form_newsletter_signin', // string | false | |
fields: [ | |
{'name' : {'selector': '#field_name_ft', validation: 'required'}}, | |
{'email' : {'selector': '#field_email_ft', validation: 'required'}} | |
], | |
onInit: function(form){}, | |
onValidation: function(form, fields){}, | |
onInvalid: function(form){ | |
form.message('danger', 'Preencha todos os campos.'); | |
}, | |
onError: function(form, response){}, | |
onSuccess: function(form, response){}, | |
onSendStart: function(form){}, | |
onSendEnd: function(form, response){}, | |
filterData: function(data, form){ return data; } | |
* }); | |
* @dependsOn https://gist.github.com/bruno-barros/98db76c8d777caa3c414 | |
*/ | |
(function ($, window, document, undefined) { | |
"use strict"; | |
// Create the defaults once | |
var defaults = { | |
action: "", | |
messages: { | |
container: '.response-msg', | |
notValid: 'Preencha os campos obrigatórios.' | |
}, | |
fields: [], | |
onInit: null, | |
onSendStart: null, | |
onSendEnd: null, | |
onInvalid: null, | |
onError: null, | |
onSuccess: null | |
}; | |
// The actual plugin constructor | |
function Plugin(element, options) { | |
this.ele = element; | |
this.$ele = $(element); | |
this.fields = []; | |
this.settings = $.extend(true, {}, defaults, options); | |
this._defaults = defaults; | |
this._name = 'WelAjaxForm'; | |
this.init(); | |
} | |
// Avoid Plugin.prototype conflicts | |
$.extend(Plugin.prototype, { | |
init: function () { | |
var self = this; | |
this.mapFields(); | |
this.$alert = this.$ele.find(this.settings.messages.container); | |
this.$ele.find('button').on('click', function (e) { | |
e.preventDefault(); | |
self.sending(); | |
}); | |
// onInit event | |
if (typeof self.settings.onInit == 'function') { | |
self.settings.onInit(self); | |
} | |
}, | |
mapFields: function(additionalFields){ | |
var self = this; | |
// store fields as jQuery obj | |
$.each($.extend(true, {}, this.settings.fields, additionalFields), function (index, field) { | |
var key = Object.keys(field)[0]; | |
self.fields.push({ | |
"name": key, | |
"obj": $(field[key]['selector']), | |
"required": field[key]['validation'] == 'required' | |
}); | |
}); | |
}, | |
sending: function () { | |
var self = this; | |
if (self.validate() === false) { | |
if (typeof self.settings.onInvalid == 'function') { | |
self.settings.onInvalid(self); | |
} else { | |
self.message('danger', self.settings.messages.notValid); | |
} | |
return; | |
} | |
// onSendStart event | |
if (typeof self.settings.onSendStart == 'function') { | |
self.settings.onSendStart(self); | |
} | |
if(self.settings.action && self.settings.action.length > 1){ | |
self.setLoading(); | |
self.clearMessage(); | |
Ajax.send(self.settings.action, self.data()).then(function (response) { | |
// is error type | |
if (response.error) { | |
if (typeof self.settings.onError == 'function') { | |
self.settings.onError(self, response); | |
} else { | |
self.message('danger', response.msg); | |
} | |
} else { // on success | |
if (typeof self.settings.onSuccess == 'function') { | |
self.settings.onSuccess(self, response); | |
} else { | |
self.message('success', response.msg); | |
self.clearFormData(); | |
} | |
} | |
self.unsetLoading(); | |
// onSendEnd event | |
if (typeof self.settings.onSendEnd == 'function') { | |
self.settings.onSendEnd(self, response); | |
} | |
}); | |
} | |
}, | |
data: function data() { | |
var data = {}; | |
$.each(this.fields, function (index, field) { | |
data[field.name] = field.obj.val(); | |
}); | |
if (typeof this.settings.filterData == 'function') { | |
data = this.settings.filterData(data, this); | |
} | |
return data; | |
}, | |
validate: function () { | |
if (typeof this.settings.onValidation == 'function') { | |
return this.settings.onValidation(this, this.fields); | |
} | |
var error = false; | |
$.each(this.fields, function (index, field) { | |
if (field.required && $.trim(field.obj.val()).length === 0) { | |
error = true; | |
} | |
}); | |
return !error; | |
}, | |
setLoading: function setLoading() { | |
this.$ele.addClass('form-working'); | |
}, | |
unsetLoading: function unsetLoading() { | |
this.$ele.removeClass('form-working'); | |
}, | |
/** | |
* data-crearfield = "false" will not clear | |
*/ | |
clearFormData: function clearFormData() { | |
$.each(this.fields, function (index, field) { | |
var clear = field.obj.data('crearfield'); | |
if(clear != false){ | |
field.obj.val(''); | |
} | |
}); | |
}, | |
message: function message(_type, _msg) { | |
// remove | |
this.$alert.find('.alert').each(function () { | |
var alert = $(this); | |
alert.animate({opacity: 0, "margin-top": '-10px'}, 1000, function () { | |
alert.remove(); | |
}); | |
}); | |
var tmp = '<div class="alert alert-' + _type + '">' + _msg + '</div>'; | |
this.$alert.slideUp(function () { | |
$(this).empty().append(tmp).slideDown(); | |
}); | |
}, | |
clearMessage: function clearMessage() { | |
this.$alert.slideUp(function () { | |
$(this).empty(); | |
}); | |
} | |
}); | |
// A really lightweight plugin wrapper around the constructor, | |
// preventing against multiple instantiations | |
$.fn['WelAjaxForm'] = function (options) { | |
return this.each(function () { | |
if (!$.data(this, "plugin_WelAjaxForm")) { | |
$.data(this, "plugin_WelAjaxForm", new Plugin(this, options)); | |
} | |
}); | |
}; | |
})(jQuery, window, document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment