Palm Pre – Managing complex objects in the depot

Although the Pre comes with SQLite under the hood, if you just want to save small data items, it seems a bit of a long convoluted drawn out bore to start using the database. To make your life easier, Mojo has a ‘Depot’ object which will allow basic saving and retrieval of data. However, this comes at a price…
The bottom line – you can only store and retrieve simple objects.

This is not strictly true. Consider the following…

function Message(message){
this.key = message.key;
this.text = message.text;
this.name = "Message"+message.key;
this.objectType = "Message";
this.lastUsed = null;
};

This is a JavaScript message object I needed for ‘Quick Message’. Instantiation is done by passing in an anonymous type like so …

var newMessage = new Message( { key: “key1”, text: “message text”});

Technically, this is not a simple type, yet the depot will save and retrieve this quite happily. The problem will start when you either use a ‘Date’ object or start attaching functionality to the object using ‘prototype’.
Let’s start with the Date object. Without actual confirmation, I can’t say, but these aren’t stored in with the depot. Saving and retrieval of this won’t fail, you’ll just get null returned.

What about saving it as a string and converting it when needed?
As I need them quite a lot around the application, I need a more OO way of doing this. The Message object needs to be as ‘standalone’ as possible, so I can create one anywhere. I don’t want to have to create one or retrieve one, then keep calculating a date based on the string.

So, l left the original field in there and created a corresponding ‘DateString’ property as well.

function Message(message){
this.key = message.key;
this.text = message.text;
this.name = "Message"+message.key;
this.objectType = "Message";
this.lastUsed = null;
this.lastUsedDateString = "Not previously used";
};

Doing this will allow me to retrieve a date string in ‘lastUsedDateString’, then convert it how I want to when needed.

The final piece of functionality, is to determine if the anonymous type passed in contains a property called ‘lastUsedDateString’ and if so, assign it to our new object.

function Message(message){
this.key = message.key;
this.text = message.text;
this.name = "Message"+message.key;
this.objectType = "Message";
this.lastUsed = null;
this.lastUsedDateString = "Not previously used";
if (message.lastUsedDateString != undefined){
this.lastUsedDateString = message.lastUsedDateString;
}
};

This is still of no use when retrieving ‘Date’ type objects. So, I’ve extended the Message object using prototype to provide a method to populate the ‘lastUsed’ property using the ‘lastUsedDateString’ immediately.

Message.prototype.ReCalcDate = function(){
if ((this.lastUsed == null) && (this.lastUsedDateString != "Not previously used" )){
this.lastUsed = new Date(this.lastUsedDateString);
}
};

You think you had problems before with complex objects, you have now. Functions are most definitely not persisted using the depot.

This means that when you retrieve your Message for the Depot, the ‘lastUsedDataString’ is populated ok, but if you then tried …

testMessage.ReCalcDate();

…it would fail, because the depot will not support the function. At this point, it simply doesn’t exist.

There is, however a neat little way of getting around this. Remember the anonymous type I used to populate the message when created. Rather than using the response from the depot as the data, passing the data into a new message will provide enough information for the new Message objects to be created, populated with the data from the Depot, and any prototype methods will be available.

testMessage = new Message(testMessage);
testMessage.ReCalcDate();

As an added bonus, if we move the ReCalcDate() call into the object creation itself, it’ll always be done for us.

function Message(message){
this.key = message.key;
this.text = message.text;
this.name = "Message"+message.key;
this.objectType = "Message";
this.lastUsed = null;
this.lastUsedDateString = "Not previously used";
if (message.lastUsedDateString != undefined){
this.lastUsedDateString = message.lastUsedDateString;
}
this.ReCalcDate();
};

Of course, if you change either of lastUsed, or lastUsedDateString you’ll need to keep them in sync, but you don’t need me for that.

Advertisements
This entry was posted in Depot, javascript, Palm Pre Dev, WebOS. Bookmark the permalink.

One Response to Palm Pre – Managing complex objects in the depot

  1. Pingback: Palm Pre – Managing complex objects in the depot (via The fantastic world of Palm Pre Dev) | The Coding Bees

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s