4 * JSON handling code for the Rico Live Grid written by
5 * Jeremy Green. Adapted from code by Richard Cowin
8 * (c) 2005-2009 Richard Cowin (http://openrico.org)
9 * (c) 2005-2009 Matt Brown (http://dowdybrown.com)
10 * (c) 2008-2009 Jeremy Green (http://www.webEprint.com)
12 * Rico is licensed under the Apache License, Version 2.0 (the "License"); you may not use this
13 * file except in compliance with the License. You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
17 * Unless required by applicable law or agreed to in writing, software distributed under the
18 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
19 * either express or implied. See the License for the specific language governing permissions
20 * and limitations under the License.
24 Rico.Buffer.AjaxJSON = Class.create(
25 /** @lends Rico.Buffer.AjaxJSON# */
28 * @class Implements a LiveGrid buffer that can make
29 * AJAX calls to the server and process responses in
30 * JSON format. Extended from Rico.Buffer.AjaxSQL.
34 * The data consumed by this buffer should be a JavaScript
35 * hash type object. The format closely follows the XML based
36 * data consumed by the Rico.Buffer.AjaxSQL buffer.
48 * The 'rows' value object of the data object is
49 * a normal JS Array with each element also being an array.
54 * buffer=new Rico.Buffer.AjaxJSON(jsonUrl, bufferopts);
56 * jsonUrl should return a string in the above format. It will
57 * be parsed into JS objects.
59 * @extends Rico.Buffer.AjaxSQL
62 initialize: function(url,options,ajaxOptions) {
63 Object.extend(this, new Rico.Buffer.AjaxSQL());
64 Object.extend(this, new Rico.Buffer.AjaxJSONMethods());
66 Object.extend(this.options, options || {});
67 Object.extend(this.ajaxOptions, ajaxOptions || {});
74 Rico.Buffer.AjaxJSONMethods = function() {};
76 Rico.Buffer.AjaxJSONMethods.prototype = {
77 /** @lends Rico.Buffer.AjaxJSON# */
79 processResponse: function(startPos,request) {
80 var json = request.responseText.evalJSON(true);
81 if (!json || json == null) {
82 Rico.writeDebugMsg("jsonAjaxUpdate: json conversion failed");
87 for (var i=0; i<json.debug.length; i++)
88 Rico.writeDebugMsg("debug msg "+i+": "+json.debug[i]);
91 alert("Data provider returned an error:\n"+json.error);
92 Rico.writeDebugMsg("Data provider returned an error:\n"+json.error);
97 var newRows = this.loadRows(json);
98 if (newRows==null) return false;
99 this.rcvdRows = newRows.length;
101 this.updateBuffer(startPos, newRows);
105 loadRows: function(json) {
106 Rico.writeDebugMsg("loadRows");
108 this.rcvdRowCount = false;
109 var rowsElement = json.get("rows");
110 var rowcnt = json.get("rowCount");
113 this.rowcntContent = rowcnt;
114 this.rcvdRowCount = true;
115 this.foundRowCount = true;
116 Rico.writeDebugMsg("loadRows, found RowCount="+this.rowcntContent);
118 this.updateUI = json.get("update_ui");
119 this.rcvdOffset = json.get("offset");
120 Rico.writeDebugMsg("loadRows, rcvdOffset="+this.rcvdOffset);
121 if (this.options.template)
122 return this.template2jsTable(json,this.options.template);
125 //return this.json2jsTable(json);
128 json2jsTable: function(json,firstRow) {
129 var newRows = new Array();
130 var trs = json.get("rows");
133 var acceptAttr=this.options.acceptAttr;
134 trs.each(function(rowData){
135 var row = new Array();
136 //var rowData = $H(pair.value);
137 rowData = $H(rowData);
139 rowData.each(function(p2){
141 row[j].content=p2.value;
142 for (var k=0; k<acceptAttr.length; k++)
143 row[j]['_'+acceptAttr[k]]="";
152 template2jsTable: function(json,template){
153 var trs = json.get("rows");
155 var rowsString = '<table>';
156 trs.each(function(rowData){
157 rowsString += template.evaluate(rowData);
159 rowsString += '</table>';
160 var rowDom = this.string2DOM(rowsString);
161 return this.dom2jstable(rowDom);
164 string2DOM: function(string){
166 try{ //Internet Explorer
167 xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
168 xmlDoc.async="false";
169 xmlDoc.loadXML(string);
171 try { //Firefox, Mozilla, Opera, etc.
172 var parser=new DOMParser();
173 xmlDoc=parser.parseFromString(string,"text/xml");
178 var el = document._importNode(xmlDoc.childNodes[0],true);
184 Rico.includeLoaded('ricoLiveGridJSON.js');