www.webOShelp.net

Get the latest on:webOS Developers' RSS FeedwebOS Facebook page webOS Twitter Feed

Mojo.Depot

A Mojo wrapper to the HTML5 Database API.  Includes basic functions to create and open databases and get and store simple objects. Designed to simplify things for people not interested in "getting their hands dirty" in the full API.

Example usage 1:

var db= new Mojo.Depot ({name:”myDB”,version:1,replace:false}, this.openOK, this.openFail);

Example usage 2:

// Open the database to get the most recent feed list 
//
db = new Mojo.Depot({name:"feedDB", version:1, estimatedSize:25000, replace: false},
this.dbOpenOK.bind(this), this.dbOpenFail.bind(this));

The dbOpenOK() function is called if the Mojo.Depot() request succeeds.

//    dbOpenOK - Callback for successful DB request in setup. Does two things: 
// Get feedlist from DB: call updateList if successful,
// otherwise useDefaultList for default set of feeds
//
FeedListAssistant.prototype.dbOpenOK = function() {
Mojo.Log.info("........","Database opened OK");
db.simpleGet("feedList", this.updateList.bind(this),
this.useDefaultList.bind(this));
};

The dbOpenFail() function is called if the Mojo.Depot() request fails.

//    dbOpenFail - Callback for failed DB open during setup. Alerts user and continues.  // 
FeedListAssistant.prototype.dbOpenFail = function(transaction, result) { 
    Mojo.Log.warn("Can't open feed database (#", result.message, "). All feeds will be reloaded."); 
    Mojo.Controller.errorDialog("Can't open feed database (#" + result.message + "). All feeds will be reloaded."); 
 }; 

The UpdateList function is called if the simpleGet() call in the dbOpenOK() function succeeds:

//    updateList - Callback for successful DB retrieval of feedlist. Call 
// useDefaultList if the feedlist empty or null or initiate an update // to the list by calling updateFeedList.
//
FeedListAssistant.prototype.updateList = function(fl) {
Mojo.Log.info("........","database size: " , Object.values(fl).size());
if (Object.toJSON(fl) == "{}" || fl === null) {
Mojo.Log.info("Retrieved empty or null list from DB");
featureIndexFeed = 0;
this.useDefaultList();
} else {
Mojo.Log.info("........","Retrieved feedlist from DB");
feedList = fl;
this.feedWgtModel.items = feedList;
this.controller.modelChanged(this.feedWgtModel);
// If stories exist in the featureIndexFeed, then start the rotation
//
if(featureIndexFeed < feedList[featureIndexFeed].stories.length) {
$("featuredFeedLabel").innerHTML = feedList[featureIndexFeed].title;
this.showFeatureStory();
}
this.updateFeedList();
}
};

useDefaultList() is called if the simpleGet() call in the dbOpenOK() function fails:

//    useDefaultList - Callback for failed DB retrieval meaning no list so use default.
//
FeedListAssistant.prototype.useDefaultList = function() {
// Couldn't get the list of feeds. Maybe its never been set up, so initialize it
// here to the default list and then initiate an update with this feed list.
//
db.simpleAdd("feedList", feedList,
function() {Mojo.Log.info("........","feedList saved OK");},
function(transaction,result) {
Mojo.Log.warn("Database save error (#", result.message,
") - can't save feed list. Will need to reload on next use.");
Mojo.Controller.errorDialog("Database save error (#" +
result.message + ") - can't save feed list. Will need to reload on next use."); });
Mojo.Log.warn("Database has no feed list. Will use default.");
this.updateFeedList();

simpleAdd() is used to update the depot when the data changes:

db.simpleAdd("feedList", feedList, 
function() {Mojo.Log.info("........","feedList saved OK");},
function(transaction,result) {
Mojo.Log.warn("Database save error (#", result.message, ") - can't save feed list. Will need to reload on next use."); Mojo.Controller.errorDialog("Database save error (#" + result.message + ") - can't save feed list. Will need to reload on next use."); });

To delete a depot object:

db.removeAll(); 
 

0 Comments

    Add Comment


      • >:o
      • :-[
      • :'(
      • :-(
      • :-D
      • :-*
      • :-)
      • :P
      • :\
      • 8-)
      • ;-)