Search code examples
htmloopmootools

return instance of database in javascript


Hi There best collegue,

i'm doing some experience with Object Oriented Javascript in combination with Mootools and creating a todolist for exercise. i've the following issue.

i've a class called database and looks as followd

var Database = new Class({

initialize:function(db){
    this.db = db;
    this.prepareDatabase();
    this.createTable();
},

/*
 * Creates the todo database
 * @return {object Database}
 * alDone ensures that the function will be called once, because we only want one database
 * with the name 'Todo' 
 */
prepareDatabase:function(){
    var alDone = false;

    if(!alDone){
     var dbSize = 5 * 1024 * 1024; // 5MB
     this.db = openDatabase("Todo", "1.0", "Todo manager", dbSize);

    }else{
        alDone = true;
    }
},

/*
 * Creates table 'todo'
 * alDone ensures that the function will be called once, because we only want one table
 * with the name 'todo' 
 */
createTable:function(){
    var alDone = false;

    if(!alDone){
        this.db.transaction(function(tx){
            tx.executeSql("CREATE TABLE IF NOT EXISTS todo(ID INTEGER PRIMARY KEY ASC, title TEXT, description TEXT, isDone INTEGER, date DATETIME)", [])
        });
    }else{
        alDone = true;
        console.log("functie createTable is al aangeroepen");
    }
},  

});

and i've created a class todo. the goal I want to achieve is to create a instance of the db and call this in a method in the todolist class. I would be gratefull if someone can help me with this issue.

Tnx in advance.

my todolist class looks like below.

var Todo = new Class(
{
initialize: function(title,description,isDone,date){
    this.title = title;
    this.description = description;
    this.isDone = isDone;
    this.date = date;
},

getTitle:function()
{       
    return this.title;  
},

getDescription:function()
{
    return this.description;
},

getIsDone:function()
{
    return this.isDone;
},

setIsDone:function(value)
{
    this.isDone = value;
},

});

Solution

  • I did not understand what you are trying to do here but here is some errors you have:

    1. In class Database you get db param in the constructor and set it to a class property name db: this.db = db; - Then you call prepareDatabase which override that this.db by setting it to openDatabase("Todo", "1.0", "Todo manager", dbSize); - so why sends the constructor db param if you override it in the next operation ?
    2. you want the functions prepareDatabase and createTable will be called once so you set a variable alDone - but this variable is local to the function so in each call to one of this functions alDone will always be false and get executed - what I guess you want is set a class variable that each call to the function wil "remember" it's state so you can do something like this:

    end List item (this is stackoverflow bug :))

    prepareDatabase:function(){
                if(!this.alDone){
                 this.alDone = true;
                 var dbSize = 5 * 1024 * 1024; // 5MB
                 this.db = openDatabase("Todo", "1.0", "Todo manager", dbSize);
                }
            },
    

    Then again you can't call this.alDone twice so you need to change the name in the other function (createTable)

    Continue: If you want to make your db class "global" (the right term is static) then don't use mootools Class - use regular js objects: (you can use mootools inside of course)

    var Database = {
    
                init:function(db){
                    this.db = db;
                    this.prepareDatabase();
                    this.createTable();
                },
    
    
                prepareDatabase:function(){
    
                    if(!this.donePrepare){
                         this.donePrepare = true;
                         var dbSize = 5 * 1024 * 1024; // 5MB
                         this.db = openDatabase("Todo", "1.0", "Todo manager", dbSize);
                    }
                },
    
                createTable:function(){
    
                    if(!this.doneCreate){
                        this.doneCreate = true;
                        this.db.transaction(function(tx){
                            tx.executeSql("CREATE TABLE IF NOT EXISTS todo(ID INTEGER PRIMARY KEY ASC, title TEXT, description TEXT, isDone INTEGER, date DATETIME)", [])
                        });
                    }else{
                        console.log("functie createTable is al aangeroepen");
                    }
                }
    
    };
    

    Then you can call it from everywhere like this:

    Database.init();