Skip to content

Instantly share code, notes, and snippets.

Created May 10, 2013 01:54
Show Gist options
  • Save anonymous/5551912 to your computer and use it in GitHub Desktop.
Save anonymous/5551912 to your computer and use it in GitHub Desktop.
Tentativa de criar um plugin jquery para criar formularios de maneira simples!
// Author: Alexandre Marinho
;(function($){
function define_attributes(elm,attrs){
for (attr in attrs){
elm.attr(attr,attrs[attr])
}
}
function define_binds(elm,binds){
for(bind in binds){
elm.on(bind,binds[bind])
}
}
var methods = {
init : function( options ) {
form = $(this)
container = $('<div class="formularize" />')
return this.each(function(){
fields = options['fields']
initial = options['initial']
for(field in fields){
var type = fields[field].type || 'text'
var label = fields[field].label || field
control_group = $('<div />').addClass('control-group')
controls = $('<div />').addClass('controls')
label_tag = $('<label />')
.addClass('control-label')
.attr('for','id_'+field)
.html(label)
switch(type){
case "textarea":
input = $('<textarea />').attr('id','id_'+field).attr('name',field)
if(initial[field]){
input.val(initial[field])
}
define_attributes(input,fields[field].attrs)
define_binds(input,fields[field].binds)
label_tag.appendTo(control_group)
input.appendTo(controls)
break;
case "select":
input = $('<select />').attr('id','id_'+field).attr('name',field)
data = fields[field].source
for(opt in fields[field].source){
value = fields[field].source[opt] || opt
option = $('<option />')
.val(opt)
.html(value)
if(initial[field]==opt){
option.attr('selected','selected')
}
option.appendTo(input)
}
define_attributes(input,fields[field].attrs)
define_binds(input,fields[field].binds)
label_tag.appendTo(control_group)
input.appendTo(controls)
break;
case "checkbox":
aux = $('<input />')
.attr('id','id_'+field)
.attr('name',field)
.attr('type',type)
if(initial[field]=true){
aux.attr('checked','checked')
}
input = $('<label />')
.addClass('checkbox')
.append(aux)
.append(label)
define_attributes(input,fields[field].attrs)
define_binds(input,fields[field].binds)
input.appendTo(controls)
break;
case "radio":
input = $('<div />')
for(radio in fields[field].source){
value = fields[field].source[radio] || radio
aux1 = $('<input type="radio" />').val(value)
define_attributes(input,fields[field].attrs)
define_binds(input,fields[field].binds)
if(initial[field]==value){
aux1.attr('checked','checked')
}
aux1.attr('id','id_'+field+'-'+radio).attr('name',field)
aux2 = $('<label class="radio" />').append(aux1).append(value)
input.append(aux2)
}
label_tag.appendTo(control_group)
input.appendTo(controls)
break;
default:
input = $('<input />')
.attr('id','id_'+field)
.attr('name',field)
.attr('type',type)
if(initial[field]){
input.val(initial[field])
}
define_attributes(input,fields[field].attrs)
define_binds(input,fields[field].binds)
label_tag.appendTo(control_group)
input.appendTo(controls)
}
controls.appendTo(control_group)
control_group.appendTo(container)
}
container.prependTo(form)
form.submit(function(){
console.log(form.serialize())
return false
})
})
},
add : function( ) {
// add field
}
};
$.fn.formularize = function(method,options) {
var settings = $.extend({
},options)
// Method calling logic
if ( methods[method] ) {
return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));
} else if ( typeof method === 'object' || ! method ) {
return methods.init.apply( this, arguments );
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.formularize' );
}
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment