Javascript Class and scope
I want to make a class in javascript to reuse from my main code in the
connection with an indexeddb object. What I have now is:
function DATABASE() {
this.DB_NAME = 'MYdatabase';
this.DB_VERSION = 1;
this.db = null;
this.results = null;
}
DATABASE.prototype.open = function(callback) {
var req = indexedDB.open(this.DB_NAME, this.DB_VERSION);
req.onsuccess = function (evt) {
this.db = this.result;
callback();
};
req.onerror = function (evt) {
console.error("openDb:", evt.target.errorCode);
};
req.onupgradeneeded = function (evt) {
console.log("openDb.onupgradeneeded");
};
}
My problem here is that when the onsuccess executes I loose the scope of
my main class and this is not what I expected. How can I do what I am
looking for? I want to make some connections at the same time with this,
something like:
var DB = new DATABASE(); DB.open(function(res){});
var DB2 = new DATABASE(); DB2.open(function(res){});
var DB3 = new DATABASE(); DB3.open(function(res){});
thanks so much.
No comments:
Post a Comment