-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathjquery.serializecfjson-0.2.js
86 lines (86 loc) · 2.03 KB
/
jquery.serializecfjson-0.2.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* TEMPLATE
* jquery.serializecfjson.js
*
* PURPOSE
* A jQuery function that will parse the JSON returned from an ajax call to a remote
* ColdFusion method and recursively convert any ColdFusion query object into a
* standard JSON recordset, as recognized by a great number of plugins and libraries.
*
* Standard native ColdFusion query object, as rendered by it's 'json' returnFormat:
<pre><code>
{
"COLUMNS":["ID","NAME","EMAIL"],
"DATA":[
[1,"Ed Spencer","[email protected]"],
[2,"Abe Elias","[email protected]"],
[3,"Cutter","[email protected]"]
]
}
</code></pre>
*
* converted by the method
<pre><code>
{
"COLUMNS":["ID","NAME","EMAIL"],
"DATA":[
[1,"Ed Spencer","[email protected]"],
[2,"Abe Elias","[email protected]"],
[3,"Cutter","[email protected]"]
]
}
[
{"id":1,"name":"Ed Spencer","email":"[email protected]"},
{"id":2,"name":"Abe Elias","email":"[email protected]"},
{"id":3,"name":"Cutter","email":"[email protected]"}
]
</code></pre>
* USAGE
* var populateGrid = function (postdata) {
* $.ajax({
* url: '/com/cc/Blog/Entries.cfc',
* data: {
* method: 'GetEntries',
* returnFormat: 'json'
* },
* method:'POST',
* dataType:"json",
* success: function(d,r,o){
* d = $.serializeCFJSON(d);
* if(d.success){
* // do something with the data
* } else {
* console.log(d.message);
* }
* }
* });
* };
*/
(function( $ ){
$.serializeCFJSON=function(obj) {
var json = {};
$.each(obj,function(ind, el){
switch(typeof el){
case 'object':
if(el.DATA !== undefined && el.COLUMNS !== undefined){
var recArr = [];
$.each(el.DATA,function(ind,ele){
var rec = new Object();
$.each(el.COLUMNS,function(pos,nm){
rec[nm.toLowerCase()] = ele[pos];
});
recArr.push(rec);
});
json[ind] = recArr;
} else {
json[ind] = $.serializeCFJSON(el);
}
break;
default:
json[ind] = el;
break;
}
});
return json;
};
})( jQuery );