]>
Commit | Line | Data |
---|---|---|
0bb29d35 | 1 | /* A reader to store a single JSON Object (hash) into a storage. |
13fc756d | 2 | * Also accepts an array containing a single hash. |
0bb29d35 DM |
3 | * |
4 | * So it can read: | |
5 | * | |
13fc756d | 6 | * example1: {data1: "xyz", data2: "abc"} |
0bb29d35 DM |
7 | * returns [{key: "data1", value: "xyz"}, {key: "data2", value: "abc"}] |
8 | * | |
13fc756d | 9 | * example2: [ {data1: "xyz", data2: "abc"} ] |
0bb29d35 DM |
10 | * returns [{key: "data1", value: "xyz"}, {key: "data2", value: "abc"}] |
11 | * | |
017a6376 | 12 | * If you set 'readArray', the reader expects the object as array: |
0bb29d35 DM |
13 | * |
14 | * example3: [ { key: "data1", value: "xyz", p2: "cde" }, { key: "data2", value: "abc", p2: "efg" }] | |
15 | * returns [{key: "data1", value: "xyz", p2: "cde}, {key: "data2", value: "abc", p2: "efg"}] | |
16 | * | |
17 | * Note: The records can contain additional properties (like 'p2' above) when you use 'readArray' | |
18 | * | |
19 | * Additional feature: specify allowed properties with default values with 'rows' object | |
20 | * | |
05a977a2 | 21 | * let rows = { |
0bb29d35 DM |
22 | * memory: { |
23 | * required: true, | |
24 | * defaultValue: 512 | |
25 | * } | |
26 | * } | |
27 | * | |
28 | */ | |
29 | ||
30 | Ext.define('Proxmox.data.reader.JsonObject', { | |
31 | extend: 'Ext.data.reader.Json', | |
01031528 | 32 | alias: 'reader.jsonobject', |
13fc756d | 33 | |
0bb29d35 | 34 | readArray: false, |
fba35462 | 35 | responseType: undefined, |
0bb29d35 DM |
36 | |
37 | rows: undefined, | |
38 | ||
39 | constructor: function(config) { | |
05a977a2 | 40 | let me = this; |
0bb29d35 DM |
41 | |
42 | Ext.apply(me, config || {}); | |
43 | ||
44 | me.callParent([config]); | |
45 | }, | |
46 | ||
47 | getResponseData: function(response) { | |
05a977a2 | 48 | let me = this; |
0bb29d35 | 49 | |
05a977a2 | 50 | let data = []; |
0bb29d35 | 51 | try { |
bf51bc49 TL |
52 | let result = Ext.decode(response.responseText); |
53 | // get our data items inside the server response | |
54 | let root = result[me.getRootProperty()]; | |
0bb29d35 DM |
55 | |
56 | if (me.readArray) { | |
fe8855e0 TL |
57 | // it can be more convenient for the backend to return null instead of an empty array |
58 | if (root === null) { | |
59 | root = []; | |
60 | } | |
05a977a2 | 61 | let rec_hash = {}; |
0bb29d35 DM |
62 | Ext.Array.each(root, function(rec) { |
63 | if (Ext.isDefined(rec.key)) { | |
64 | rec_hash[rec.key] = rec; | |
65 | } | |
66 | }); | |
67 | ||
68 | if (me.rows) { | |
69 | Ext.Object.each(me.rows, function(key, rowdef) { | |
05a977a2 | 70 | let rec = rec_hash[key]; |
0bb29d35 DM |
71 | if (Ext.isDefined(rec)) { |
72 | if (!Ext.isDefined(rec.value)) { | |
73 | rec.value = rowdef.defaultValue; | |
74 | } | |
75 | data.push(rec); | |
76 | } else if (Ext.isDefined(rowdef.defaultValue)) { | |
01031528 | 77 | data.push({ key: key, value: rowdef.defaultValue }); |
0bb29d35 | 78 | } else if (rowdef.required) { |
01031528 | 79 | data.push({ key: key, value: undefined }); |
0bb29d35 DM |
80 | } |
81 | }); | |
82 | } else { | |
83 | Ext.Array.each(root, function(rec) { | |
84 | if (Ext.isDefined(rec.key)) { | |
85 | data.push(rec); | |
86 | } | |
87 | }); | |
88 | } | |
13fc756d | 89 | } else { |
fe8855e0 TL |
90 | // it can be more convenient for the backend to return null instead of an empty object |
91 | if (root === null) { | |
92 | root = {}; | |
93 | } else if (Ext.isArray(root)) { | |
05a977a2 | 94 | if (root.length === 1) { |
fe8855e0 | 95 | root = root[0]; |
0bb29d35 DM |
96 | } else { |
97 | root = {}; | |
98 | } | |
99 | } | |
100 | ||
101 | if (me.rows) { | |
102 | Ext.Object.each(me.rows, function(key, rowdef) { | |
103 | if (Ext.isDefined(root[key])) { | |
01031528 | 104 | data.push({ key: key, value: root[key] }); |
0bb29d35 | 105 | } else if (Ext.isDefined(rowdef.defaultValue)) { |
01031528 | 106 | data.push({ key: key, value: rowdef.defaultValue }); |
0bb29d35 | 107 | } else if (rowdef.required) { |
01031528 | 108 | data.push({ key: key, value: undefined }); |
0bb29d35 DM |
109 | } |
110 | }); | |
111 | } else { | |
112 | Ext.Object.each(root, function(key, value) { | |
01031528 | 113 | data.push({ key: key, value: value }); |
0bb29d35 DM |
114 | }); |
115 | } | |
116 | } | |
01031528 | 117 | } catch (ex) { |
bf51bc49 TL |
118 | Ext.Error.raise({ |
119 | response: response, | |
120 | json: response.responseText, | |
121 | parseError: ex, | |
122 | msg: 'Unable to parse the JSON returned by the server: ' + ex.toString(), | |
123 | }); | |
124 | } | |
0bb29d35 DM |
125 | |
126 | return data; | |
01031528 | 127 | }, |
0bb29d35 | 128 | }); |