• selinov

Simple model is best!

Flojuggler for IOS on Github

So I’m sitting there looking at my model class for the Flojuggler app and wondering at how simple it is. Instinctually I worried that it was too simple and needed some data exchanging panache but then I remembered that a good model should be simple.

var Model = {

 openDB : function () {
 if (window.openDatabase) {
 db = window.openDatabase("flos","1.0","Local database of flos", 1024*1000);
 db.transaction(function(tx) {
 tx.executeSql("CREATE TABLE IF NOT EXISTS Flos (id INTEGER PRIMARY KEY, name TEXT, cycle INTEGER, long INTEGER, startDate DATE, thumbnail TEXT)", []);
 });
 } else {
 console.log("No browser support for DB");
 }
 },

 addFlo : function(e) {
 e = $.parseJSON(e);

 db.transaction(
 function(tx) {
 tx.executeSql("INSERT INTO Flos (name,cycle,long,startDate,thumbnail) VALUES (?,?,?,?,?)", [e.name,e.cycle,e.long,e.startDate,e.thumbnail], displayInsertResult);
 },

 function(err) {
 console.log("Error: " + err.message);
 }
 );
 },

 updateFlo : function(e) {
 e = $.parseJSON(e);

 db.transaction(
 function(tx) {
 tx.executeSql("UPDATE Flos SET name=?,cycle=?,long=?,startDate=?,thumbnail=? WHERE id = " + currentFlo, [e.name,e.cycle,e.long,e.startDate,e.thumbnail]);
 },

 function(err) {
 console.log("Error: " + err.message);
 }
 );

 },

 deleteFlo : function(id) {
 db.transaction(
 function(tx) {
 tx.executeSql("DELETE FROM Flos WHERE id = ?", [id], displayDeleteResult);
 },

 function(err) {
 console.log("Error: " + err.message);
 }
 );

 },

 getFlos : function() {
 db.transaction(
 function(tx) {
 tx.executeSql("SELECT * FROM Flos", [], initFlos);
 });
 },

 getFlo : function(id) {
 db.transaction(
 function(tx) {
 tx.executeSql("SELECT * FROM Flos WHERE id = ?", [id], populateEditPage);
 });
 },

 clearTable: function() {
 db.transaction(function (tx) {
 tx.executeSql("DROP TABLE Flos");
 });
 }
} //end model

In retrospect it seems like the getFlo method is unnecessary so I’m refactoring to remove it. There’s no need to do another database read if I already pulled the set with the getFlos method.

The clearTable method is more of  helper function than anything else. It’ll likely be removed before I ship this.

© 2020