Now when everything works, we can think of some useful ingredients. Data saving is one of the things that seem obvious. In my personal experience, data loss is the nastiest thing that may happen (network connection loss, program error, some accidentally pressed button - losing is much easier than creating). In case of using Google Gear, you can store all the information on client's side. Then if a page is reloaded or even closed, the data won't be lost. During your next visit the data can be restored out of the local storage. Unlike grid, Gears's initialization and configuring are troublesome. As a first step we need to install Gears and create a database.
Code (javascript)
if (!window.google) return alert("The page requires Google Gears \n It will work without it as well, but you will not be able to work offline.");
db = google.gears.factory.create(‘beta.database’);
if (db) {
db.open(‘gearsgrid’);
db.execute(‘create table if not exists photodata (pkID varchar(255), Name varchar(255), Manufacturer varchar(255), Format varchar(255), Horizontal varchar(255), Vertical varchar(255), Zoom varchar(255), varchar(255), Viewfinder varchar(255), LCD varchar(255), Weight varchar(255), Width varchar(255), Height varchar(255), Depth varchar(255), Known varchar(255), Link varchar(255), updated int)’);
var rs = db.execute(’select count(*) from photodata’);
if (rs.field(0)==0){
var request = google.gears.factory.create(‘beta.httprequest’);
request.open(‘GET’, ‘data.csv?uid=’+(new Date()).valueOf());
request.onreadystatechange = function() {
if (request.readyState == 4){
var data=request.responseText.split("\n");
for (var i=0; i < data.length; i++){
var fields=data[i].split(";")
fields.push(0)
db.execute("insert into photodata values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)",fields);
};
fill_grid();
}
};
request.send();
}
rs.close();
}
If you don't receive any data working with the object “request”,
check the character encoding in the loaded document.
If it is not Us-ASCII | Unicode, “request” will return null - such a nice peculiarity.
As promised - this code fragment came out to be fairly big, but it is not complicated. There's just a lot of text in it. The Gears loaded the data into the local database, and now the grid can be populated with this data. Code (javascript)
function fill_grid(){
if (db){ //if there is Google Gears
var data=[];
var rs=db.execute("select * from photodata");
while (rs.isValidRow()) {
data.push({
id:rs.field(0), * <code javascript> data:[rs.field(1), rs.field(2), rs.field(3), rs.field(4), rs.field(5), rs.field(6), rs.field(7), rs.field(8), rs.field(9), rs.field(10), rs.field(11), rs.field(12), rs.field(13), rs.field(14), rs.field(15)]});
rs.next();
}
rs.close();
grid.clearAll();
grid.parse({rows:data},"json")
} else
}
Two examples described above allow us proxying the data through Google Gears. But there's no other particular use of this, except for some traffic saving (when the second loading takes place, the data will be loaded from the local data store). Though if you load huge datasets, the saving can be substantial. There is one step left to make Gears really wholesome: Code (javascript)
* <code javascript>
grid.attachEvent(“onEditCell”,function(stage,id,ind,value){ </code>
if (stage==2){
grid.setRowTextStyle(id,"font-weight:bold;");
if (db) //save a new cell value in the local data store
db.execute("update photodata set "+columns[ind]+"=?, updated=1 where pkID=?",[value,id]);
}
return true;
})
The result is available here If GoogleGears is not installed - everything continues to work as it was with the previous examples. If GoogleGears is installed, everything gets rather interesting: edit the cell and press F5. When the page is reloaded, new edited data will be presented in grid. You might just as well close the browser window and visit the same page using some other browser - edited data will still be there. Whatever the software and the network connection troubles are, there's no way to lose your data!