Skip to content

Instantly share code, notes, and snippets.

@pbse
Last active July 14, 2016 15:57
Show Gist options
  • Select an option

  • Save pbse/2d07a676a563691ae1aa1b24eddcb375 to your computer and use it in GitHub Desktop.

Select an option

Save pbse/2d07a676a563691ae1aa1b24eddcb375 to your computer and use it in GitHub Desktop.
Revealing Module Pattern
var userModule = (function() {
var db; // Private Global Variable
function createTable(transaction) {
transaction.executeSql('create table if not exists diary(id INTEGER PRIMARY KEY AUTOINCREMENT, userName TEXT, published DATE)')
};
function errorHandler(e) {
log(e);
};
function init(callback) {
try {
db = window.openDatabase("user", 1.0, "user", 2 * 1024 * 1024);
db.transaction(createTable, errorHandler, callback);
} catch (e) {
errorhandler(e);
}
};
function storeUser(userDetails, callback) {
db.transaction(function(t) {
t.executeSql('insert into user(userName,published) values(?,?)', [userDetails.userName, new Date().getTime()],
function() {
callback();
}, errorHandler);
}, errorHandler);
};
function getUser(id, callback) {
db.transaction(function(t) {
t.executeSql('select from user where id = ?', [id],
function(t, results) {
callback(results);
}, errorHandler);
}, errorHandler);
};
function log(msg) {
console.log(msg);
};
// Anything returned will become public.
return {
init: init,
getUser: getUser,
storeUser: storeUser
}
})();
// Usage
userModule.init(someFunction);
userModule.storeUser({
userName: 'Prashant'
}, someFunction);
userModule.getUser(1, someFunction)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment