In this tutorial we are going to cover using the Depot as a data source for a list. We will be expanding on the code written in the previous parts of this tutorial listed below.
Part 1: Mojo Lists
Part 2: Mojo Lists: Using a Cookie as a Data Source
Part 3: Mojo Lists: Restructuring our Code
If you were able to follow along with using the cookie as a data source this is going to be relatively easy for you to integrate into the existing code.
In Part 2 of this series we created the ability to use a cookie for a lists data source. In this part we are going to cleanup our code a bit and try to restructure things so that it is easier to expand on our set of code.
After looking at the code I saw that we created the WBCookie model object that was not very abstract. We wish to keep things somewhat abstract so that the application is not concerned too much about where the data is coming from. We are going to be creating a few files that will be in our lib/ directory and I will explain why we are using them and what they do as we continue through the tutorial.
The first thing I want you to do is open up your sources.json file and add 3 lines at the top. We will want to include the files that we will be creating so that Mojo will know where to find them. Open your file and make it look like the following. Note that I only added the first three lines to this file.
Video of Douglas Crockford's talk at MIT still pending, meanwhile enjoy him here from Google http://hex.io/1i6f plus some follow-along slides.
In Part 1 of this tutorial we created the base for our application. It consisted of a static list that was displayed when the application was launched.
At the end of Part 1 you were able to build the application and tap on any of the items which would push another scene. In Part 2 of this tutorial we are going to focus on using a cookie as a data source for our list. We will build a list that will display a list based on a cookie's contents.
When you tap on a list item you will be taken to another scene where you will be able to change the title for the list item and it will be retained in a cookie. I did find an issue with cookies on the emulator and device. There is a known bug that cookies will retain on the emulator and device even after the application is removed. To learn more about this issue and cookies I suggest you read the Working with Cookies tutorial.
We will be continuing with the code that we created in Part 1 so open that application up. We will first be creating a model object that we will use to interact with the Mojo cookie stuff. In your application create the folder /app/lib/ and then create a new file named WBCookie.js and put it in your newly created lib folder. Now you will also have to update your sources.json so that Mojo will know where to find this file. Be sure that your sources.json file looks like the code below.
[
{ "source": "app\/lib\/WBCookie.js" },
{"source": "app\/assistants\/stage-assistant.js"},
{
"source": "app\/assistants\/main-assistant.js",
"scenes": "main"
},
{
"source": "app\/assistants\/cookie-assistant.js",
"scenes": "cookie"
},
{
"source": "app\/assistants\/depot-assistant.js",
"scenes": "depot"
},
{
"source": "app\/assistants\/sqlite-assistant.js",
"scenes": "sqlite"
}
]
Douglas Crockford , author of "Javascript: The Good Parts" from O'Reilly Media - is speaking TODAY (Friday) at MIT at 12:45 in room G449 of the Patil/ Kiva Seminar Room in the Stata center (building closest to Vassar Street.) - this is the same building we met for the Sept 8th meeting. I have his book, and can vouch for it's excellence!
So, if you can get out for ONE HOUR come and listen in!
Cost: none
TIME:Refreshments: 12:45
Talk: 1: 00 PM to 2pm
At the request of a reader this next tutorial will cover Mojo Lists however it will get quite long so I have decided to break it into smaller parts.
Part 1 (This section) will deal with setting up a static list.
Part 2 will show how to use a cookie as a data source.
Part 3 will be some restructuring of the code
Part 4 will show how to use the depot as a data source.
Part 5 will show how to use an Sqlite database as the source.
Source Code will be posted for download after the series is complete.
On our main scene we are going to show a static list of items. This means that we are going to use an array of elements which will never change over the lifetime of the application. The list item data will be specified by us and not fetched from any data source. The main list will give you options to choose from such as using a Cookie, Depot or Sqlite for the data source. When you tap on one of these options another scene will be displayed that will actually load another list. The data that populates these lists will come from the data source that you specified. This should give you the ability to write your own lists using any data source that you see fit.
The very first thing that you need to do is create a new WebOS application and add a new scene to it called main. Now that we have the main scene created we need to alter our stage-controller.js so that it actually pushes the main-scene on startup. Open the file and make the code look like this.
function StageAssistant() {
}
StageAssistant.prototype.setup = function() {
Mojo.Controller.stageController.pushScene("main");
}
It's not a big secret that gDial pro has been around for a while in the home brew community. What is a bit surprising and actually pleasing is that Palm has allowed it into the App Catalog. I have used the application and like just about everything there is except for the fact that it opens your phones main dialer to actually make the call. I doubt that there is currently any other way for the developer to place the call but I do hope that Palm opens the SDK to allow for this. It would be so nice to be able to just dial your number and have it dial straight in the gDial Pro application.
If you have never used gDial Pro then you need to head over to Google Voice and sign-up for an account. It is still in beta so you will need to wait for the invite from Google. When I registered a month back I only had to wait a few days to be invited but I have heard from others that they had to wait about a week. The only other thing I dislike about the interface is the colors the developer choose to use. It's pretty much all gray-scale, here's to hoping they allow for themes in the future so we can get past the grim look of the user interface as it is by default.
Over the last few days I have been working with the Cookie as a method for storage on the Palm Pre. I looked in the WebOS Book and there was a nice cookie class that could be used. This will not be a complete walk-through like the last tutorials have been. This will show you how to create a cookie object and how to interact with it so that you can store your application preferences. You can store any type of data you would like in a cookie. It does not seem to be like the Depot where you can *only* store objects.
At the time of writing I have found an issue with using cookies so be sure to read the second to last paragraph.
Below is the class that was taken from the WebOS Book which we will be using for our Cookie object.