From 644daecfc2cb908929be9e188711ed09c307cddd Mon Sep 17 00:00:00 2001 From: Marcos Mariano Date: Tue, 20 Jan 2015 00:20:26 -0200 Subject: [PATCH] Form Data Set Change module.setDefaults to module.set.field.defaults Create function module.set.field.value, module.set.field.values --- src/definitions/behaviors/form.js | 77 ++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 21 deletions(-) diff --git a/src/definitions/behaviors/form.js b/src/definitions/behaviors/form.js index 7dc03ba02..261230eb6 100644 --- a/src/definitions/behaviors/form.js +++ b/src/definitions/behaviors/form.js @@ -60,7 +60,7 @@ $.fn.form = function(fields, parameters) { initialize: function() { module.verbose('Initializing form validation', $module, validation, settings); module.bindEvents(); - module.setDefaults(); + module.set.field.defaults(); module.instantiate(); }, @@ -132,26 +132,6 @@ $.fn.form = function(fields, parameters) { ; }, - setDefaults: function() { - $field - .each(function () { - var - $field = $(this), - type = $field.prop('type') - ; - - switch (type) { - case 'checkbox': - case 'radio': - $field.data('defaultValue', $field.is(':checked')); - break; - default: - $field.data('defaultValue', $field.val()); - } - }) - ; - }, - reset: function() { $field .each(function () { @@ -433,6 +413,61 @@ $.fn.form = function(fields, parameters) { .removeClass(className.success) .addClass(className.error) ; + }, + field: { + defaults: function () { + $field + .each(function () { + var + $field = $(this), + type = $field.prop('type') + ; + + switch (type) { + case 'checkbox': + case 'radio': + $field.data('defaultValue', $field.is(':checked')); + break; + default: + $field.data('defaultValue', $field.val()); + } + }) + ; + }, + value: function (field, value) { + var data = {}; + data[field] = value; + return module.set.field.values.call(element, data); + }, + values: function (data) { + if ($.isEmptyObject(data)) + return; + + $.each(data, function (key, value) { + if (typeof value === 'object') + return; + + var + $identifier = module.get.field(key), + type = $identifier.prop('type'), + $parent = $identifier.parent() + ; + + if (!$identifier.length) + return; + + switch (type) { + case 'checkbox': + $identifier.parent('.ui.checkbox').checkbox(value ? 'check' : 'uncheck'); + break; + case 'radio': + $identifier.filter('[value="' + value + '"]').parent('.ui.checkbox').checkbox('check'); + break; + default: + $parent.is('.ui.dropdown') ? $parent.dropdown('set selected', value) : $identifier.val(value); + } + }); + } } },