Skip to content

Instantly share code, notes, and snippets.

@gustavo-rodrigues-dev
Last active December 14, 2015 06:09
Show Gist options
  • Save gustavo-rodrigues-dev/5040520 to your computer and use it in GitHub Desktop.
Save gustavo-rodrigues-dev/5040520 to your computer and use it in GitHub Desktop.
Método que serializa os dados do formulário e retorna em um objeto, array ou string (key : value); IE7++ (Não testado em versões anteriores), jQuery 1.2 ++ (serializeArray)
$('#teste').on('submit', function(e){
e.preventDefault();
var x = 0
//to string mode
console.log($('#teste').serializeTo({returnType: ''}));
//default
console.log($('#teste').serializeTo());
//callback function
$('#teste').serializeTo({returnType: []}, function(x){
console.log(x);
})
/**
* Plugin serializeTo
* return dataserialize object, array or string
* @author Gustavo da Silva Rodrigues
**/
;(function ( $, window, document, undefined ) {
var pluginName = "serializeTo",
/**
* default Configuration
* @param Object defaults
**/
defaults = {
/**
* Config
* @param Object | Array | String returnType
**/
returnType: {} // {}, [], ''
};
function Plugin ( element, options) {
//private
this.element = element;
this.settings = $.extend( {}, defaults, options );
this.dataArray = $serilizeData = $(document).find(this.element).serializeArray();
this._defaults = this.settings || defaults;
this._name = pluginName;
this.result = {};
this.init();
return this.result;
}
Plugin.prototype = {
init: function () {
var _this = this;
switch (Object.prototype.toString.call( this._defaults.returnType )){
case '[object object]' :
_this.result = {};
for(var data in this.dataArray){
var name = this.dataArray[data].name.replace('[]','');
if( typeof _this.result[name] != 'undefined' ){
if(Array.isArray(_this.result[name])){
_this.result[name].push(this.dataArray[data].value)
} else {
var temp = _this.result[name];
this.result[name] = [];
this.result[name].push(temp);
this.result[name].push(this.dataArray[data].value);
}
} else {
_this.result[name] = this.dataArray[data].value;
}
}
break;
case '[object Array]' :
_this.result = [];
for(var data in this.dataArray){
var name = this.dataArray[data].name.replace('[]','');
if( typeof _this.result[name] != 'undefined' ){
if(Array.isArray(_this.result[name])){
_this.result[name].push(this.dataArray[data].value)
} else {
var temp = _this.result[name];
this.result[name] = [];
this.result[name].push(temp);
this.result[name].push(this.dataArray[data].value);
}
} else {
_this.result[name] = this.dataArray[data].value;
}
}
break;
case '[object String]' :
_this.result = '';
for(var data in this.dataArray){
_this.result += this.dataArray[data].name +' : '+ this.dataArray[data].value +'\n';
}
break;
default:
_this.result = {};
for(var data in this.dataArray){
var name = this.dataArray[data].name.replace('[]','');
if( typeof _this.result[name] != 'undefined' ){
if(Array.isArray(_this.result[name])){
_this.result[name].push(this.dataArray[data].value)
} else {
var temp = _this.result[name];
this.result[name] = [];
this.result[name].push(temp);
this.result[name].push(this.dataArray[data].value);
}
} else {
_this.result[name] = this.dataArray[data].value;
}
}
break;
}
return _this.result;
}
};
$.fn[ pluginName ] = function ( options, callback ) {
if (typeof callback == 'function') {
return callback($.data( this, "plugin_" + pluginName, new Plugin( this, options )));
} else {
return $.data( this, "plugin_" + pluginName, new Plugin( this, options ));
}
};
})( jQuery, window, document );
/**Compatibility
* Running the following code before any other code will create Array.isArray() if it's not natively available.
* Ref.: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray
**/
(function(e,t,n,r){function o(t,r){this.element=t;this.settings=e.extend({},s,r);this.dataArray=$serilizeData=e(n).find(this.element).serializeArray();this._defaults=this.settings||s;this._name=i;this.result={};this.init();return this.result}var i="serializeTo",s={returnType:{}};o.prototype={init:function(){var e=this;switch(Object.prototype.toString.call(this._defaults.returnType)){case"[object object]":e.result={};for(var t in this.dataArray){var n=this.dataArray[t].name.replace("[]","");if(typeof e.result[n]!="undefined"){if(Array.isArray(e.result[n])){e.result[n].push(this.dataArray[t].value)}else{var r=e.result[n];this.result[n]=[];this.result[n].push(r);this.result[n].push(this.dataArray[t].value)}}else{e.result[n]=this.dataArray[t].value}}break;case"[object Array]":e.result=[];for(var t in this.dataArray){var n=this.dataArray[t].name.replace("[]","");if(typeof e.result[n]!="undefined"){if(Array.isArray(e.result[n])){e.result[n].push(this.dataArray[t].value)}else{var r=e.result[n];this.result[n]=[];this.result[n].push(r);this.result[n].push(this.dataArray[t].value)}}else{e.result[n]=this.dataArray[t].value}}break;case"[object String]":e.result="";for(var t in this.dataArray){e.result+=this.dataArray[t].name+" : "+this.dataArray[t].value+"\n"}break;default:e.result={};for(var t in this.dataArray){var n=this.dataArray[t].name.replace("[]","");if(typeof e.result[n]!="undefined"){if(Array.isArray(e.result[n])){e.result[n].push(this.dataArray[t].value)}else{var r=e.result[n];this.result[n]=[];this.result[n].push(r);this.result[n].push(this.dataArray[t].value)}}else{e.result[n]=this.dataArray[t].value}}break}return e.result}};e.fn[i]=function(t,n){if(typeof n=="function"){return n(e.data(this,"plugin_"+i,new o(this,t)))}else{return e.data(this,"plugin_"+i,new o(this,t))}}})(jQuery,window,document)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment