嗨,最好的同事,
我正在结合 Mootools 做一些面向对象的 Javascript 的经验,并创建一个用于锻炼的 todolist。我有以下问题。
我有一个名为数据库的类,看起来如下
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");
}
},
});
我创建了一个类 todo。我想要实现的目标是创建一个数据库实例并在 todolist 类的方法中调用它。如果有人可以帮助我解决这个问题,我将不胜感激。
提前Tnx。
我的 todolist 类如下所示。
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;
},
});