author | Giulio Cesare Solaroli <giulio.cesare@solaroli.it> | 2011-10-03 16:04:12 (UTC) |
---|---|---|
committer | Giulio Cesare Solaroli <giulio.cesare@solaroli.it> | 2011-10-03 16:04:12 (UTC) |
commit | 541bb378ddece2eab135a8066a16994e94436dea (patch) (unidiff) | |
tree | ff160ea3e26f7fe07fcfd401387c5a0232ca715e /frontend/beta/js/YUI-extensions/data/JSONDataModel.js | |
parent | 1bf431fd3d45cbdf4afa3e12afefe5d24f4d3bc7 (diff) | |
parent | ecad5e895831337216544e81f1a467e0c68c4a6a (diff) | |
download | clipperz-541bb378ddece2eab135a8066a16994e94436dea.zip clipperz-541bb378ddece2eab135a8066a16994e94436dea.tar.gz clipperz-541bb378ddece2eab135a8066a16994e94436dea.tar.bz2 |
Merge pull request #1 from gcsolaroli/master
First version of the restructured repository
Diffstat (limited to 'frontend/beta/js/YUI-extensions/data/JSONDataModel.js') (more/less context) (ignore whitespace changes)
-rw-r--r-- | frontend/beta/js/YUI-extensions/data/JSONDataModel.js | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/frontend/beta/js/YUI-extensions/data/JSONDataModel.js b/frontend/beta/js/YUI-extensions/data/JSONDataModel.js new file mode 100644 index 0000000..ca48dce --- a/dev/null +++ b/frontend/beta/js/YUI-extensions/data/JSONDataModel.js | |||
@@ -0,0 +1,81 @@ | |||
1 | |||
2 | /** | ||
3 | * @class YAHOO.ext.grid.JSONDataModel | ||
4 | * This is an implementation of a DataModel used by the Grid. It works | ||
5 | * with JSON data. | ||
6 | * <br>Example schema: | ||
7 | * <pre><code> | ||
8 | * var schema = { | ||
9 | * root: 'Results.Result', | ||
10 | * id: 'ASIN', | ||
11 | * fields: ['Author', 'Title', 'Manufacturer', 'ProductGroup'] | ||
12 | * }; | ||
13 | * </code></pre> | ||
14 | * @extends YAHOO.ext.grid.LoadableDataModel | ||
15 | * @constructor | ||
16 | */ | ||
17 | YAHOO.ext.grid.JSONDataModel = function(schema){ | ||
18 | YAHOO.ext.grid.JSONDataModel.superclass.constructor.call(this, YAHOO.ext.grid.LoadableDataModel.JSON); | ||
19 | /**@private*/ | ||
20 | this.schema = schema; | ||
21 | }; | ||
22 | YAHOO.extendX(YAHOO.ext.grid.JSONDataModel, YAHOO.ext.grid.LoadableDataModel, { | ||
23 | /** | ||
24 | * Overrides loadData in LoadableDataModel to process JSON data | ||
25 | * @param {Object} data The JSON object to load | ||
26 | * @param {Function} callback | ||
27 | */ | ||
28 | loadData : function(data, callback, keepExisting){ | ||
29 | var idField = this.schema.id; | ||
30 | var fields = this.schema.fields; | ||
31 | try{ | ||
32 | if(this.schema.totalProperty){ | ||
33 | var v = parseInt(eval('data.' + this.schema.totalProperty), 10); | ||
34 | if(!isNaN(v)){ | ||
35 | this.totalCount = v; | ||
36 | } | ||
37 | } | ||
38 | var rowData = []; | ||
39 | var root = eval('data.' + this.schema.root); | ||
40 | for(var i = 0; i < root.length; i++){ | ||
41 | var node = root[i]; | ||
42 | var colData = []; | ||
43 | colData.node = node; | ||
44 | colData.id = (typeof node[idField] != 'undefined' && node[idField] !== '' ? node[idField] : String(i)); | ||
45 | for(var j = 0; j < fields.length; j++) { | ||
46 | var val = node[fields[j]]; | ||
47 | if(typeof val == 'undefined'){ | ||
48 | val = ''; | ||
49 | } | ||
50 | if(this.preprocessors[j]){ | ||
51 | val = this.preprocessors[j](val); | ||
52 | } | ||
53 | colData.push(val); | ||
54 | } | ||
55 | rowData.push(colData); | ||
56 | } | ||
57 | if(keepExisting !== true){ | ||
58 | this.removeAll(); | ||
59 | } | ||
60 | this.addRows(rowData); | ||
61 | if(typeof callback == 'function'){ | ||
62 | callback(this, true); | ||
63 | } | ||
64 | this.fireLoadEvent(); | ||
65 | }catch(e){ | ||
66 | this.fireLoadException(e, null); | ||
67 | if(typeof callback == 'function'){ | ||
68 | callback(this, false); | ||
69 | } | ||
70 | } | ||
71 | }, | ||
72 | |||
73 | /** | ||
74 | * Overrides getRowId in DefaultDataModel to return the ID value of the specified node. | ||
75 | * @param {Number} rowIndex | ||
76 | * @return {Number} | ||
77 | */ | ||
78 | getRowId : function(rowIndex){ | ||
79 | return this.data[rowIndex].id; | ||
80 | } | ||
81 | }); | ||