Я разработал плагин jQuery, который сериализует элементарные типы, массивы и объекты в элементы DOM.Я до сих пор не знаю, как сериализовать замыкания.
(function($) {
$.identity = function(key, value) {
return value;
};
$.fn.tag = function(index) {
return this
.get(index || 0)
.nodeName
.toLowerCase();
};
$.fn.append2 = function(collection, callback) {
var $this = this;
// The default callback does nothing.
callback = callback || $.identity;
// Apply the callback to each element of the
// collection, and append the result.
$.each(collection, function(key, value) {
$this.append(callback(key, value));
});
return this;
};
$.serialize = function(key, value) {
if (!value) {
// No arguments? Return empty selector.
if (!key && key !== 0)
return $();
// Swap arguments.
value = key;
key = null;
}
var $this = (typeof value === 'object')
// Serialize into <div>.
? $('<div>')
.append2(value, $.serialize)
.addClass(value.constructor === Array
? 'array' : '')
// Serialize into <input>.
: $('<input>').val(value);
// Name the element.
if (key != null)
$this.attr('name', key);
return $this;
};
$.fn.deserialize = function() {
if (this.length !== 1)
return null;
var tag = this.tag();
// Deserialize into elementary value?
if (tag === 'input')
return this.val();
// Unable to deserialize?
if (tag !== 'div')
return null;
// Deserialize into array/object.
var array = this.hasClass('array');
var res = array ? [] : {};
// Deserialize members into sub-elements.
this
.children('div[name],input[name]')
.each(function() {
var $this = $(this);
var name = $this.attr('name');
var value = $this.deserialize();
res[array ? +name : name] = value;
});
return res;
};
})(jQuery);
Вот пример:
Файл 1: test.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<script type="text/javascript" src="latest-jquery.js"></script>
<script type="text/javascript" src="test.js"></script>
<title>Example</title>
</head>
<body>
<div style="display: none" id="info">
<input name="name" value="Eduardo León"/>
<input name="age" value="23"/>
<input name="degree" value="Systems Engineer"/>
<div class="array" name="relatives">
<div name="0">
<input name="relationship" value="Significant Other"/>
<input name="name" value="Kelly Cruz"/>
</div>
<div name="1">
<input name="relationship" value="Son"/>
<input name="name" value="Mauricio León"/>
</div>
</div>
</div>
</body>
</html>
Файл 2: test.js
$(document).ready(function() {
var $sel = $('#info');
// Retrieve information contained in
// the original HTML document.
var info = $sel.deserialize();
alert(info.name);
alert(info.age);
alert(info.degree);
alert(info.relatives.length);
$.each(info.relatives, function(index, relative) {
alert(relative.relationship);
alert(relative.name);
});
// Update information with a list of comics.
$sel.append($.serialize('comics', [
{name: 'xkcd', author: 'Randall Munroe'},
{name: 'Dinosaur Comics', author: 'Ryan North'}
]));
// Re-retrieve the information, including the list of comics.
info = $sel.deserialize();
$.each(info.comics, function(index, comic) {
alert(comic.name);
alert(comic.author);
});
});