forked from jquery/jquery-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.js
More file actions
52 lines (49 loc) · 1.28 KB
/
helpers.js
File metadata and controls
52 lines (49 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// demo helpers, may become part of some abstraction later
function serializeForm(form) {
var result = {};
$.each( $( form ).serializeArray(), function( index, object ) {
if ( result[ object.name ] ) {
if ( $.type( result[ object.name ] ) !== "array" ) {
result[ object.name ] = [ result[ object.name ] ];
}
result[ object.name ].push( object.value );
} else {
result[ object.name ] = object.value;
}
});
return result;
}
function deserializeForm( form, source ) {
var observable = $.observable( source );
$( form ).find("input:text").each(function() {
$( this ).val( observable.property( this.name ) );
});
}
function capitalize( value ) {
return value.replace(/^(.)(.*)$/, function(match, first, rest) {
return first.toUpperCase() + rest.toLowerCase();
});
}
function meta( input ) {
var output = [];
for ( key in input ) {
var value = input[ key ];
// replicate behaviour of JSON.stringify to skip entries
if ( value && value.toJSON && value.toJSON() === undefined ) {
continue;
}
var field = {
name: key,
label: capitalize(key),
value: value
};
if ( $.isPlainObject(field.value) ) {
for (subkey in field.value) {
field.name += "." + subkey;
field.value = field.value[ subkey ];
}
}
output.push(field);
}
return output;
}