07.15
by Andrew Tetlaw
When I last wrote the Tech Times I was quite excited about starting my first AIR application, called Harpoon, using only HTML, CSS, and JavaScript. One of the features I ran out of time to discuss was the way you can use an XML file to store and retrieve application preferences.
If you downloaded and installed Harpoon you may have noticed the Preferences panel with the select list.
Here’s the HTML for that field and its label:
<label>Refresh every (minutes)</label>
<select id="pref_refresh" name="pref_refresh">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
...
<option value="10">10</option>
</select>
If you recall, Harpoon is destined to be an auction watching application for the Flippa site. Eventually this will control how often Flippa is checked for new auctions matching your query. Of course you’d want Harpoon to remember this setting, so we’ll store the selected value in an XML file.
Our task is this:
- When Harpoon is opened we check to see if the preferences file is available.
- If unavailable Harpoon should create the file and write the default value.
- If available Harpoon should read it and set the field value.
- Whenever the field value is changed Harpoon should write the new value to the preferences file.
This sounds way more complicated than it really is. Let’s dive in so I can show you.
First some adjustments to our Harpoon object. (If you need to prod your memory, take a detour and refer back to my previous article.) We want to store the refresh value, and a couple of other important values within the Harpoon object:
var Harpoon = {
...
prefsFile : null,
storePath : null,
prefs : {
refresh : 10
}
}
I’ll address prefsFile and storePath in a moment, but you can also see the prefs object that has one property called refresh. This is where we’ll store the refresh value, and it’ll be accessible within our application from Harpoon.prefs.refresh.
Now we come back to the prefsFile and storePath properties. For security reasons each AIR application is given its own storage directory from which it can read and write files. This is where our preferences file will reside. These two properties will store the path to the application storage directory and the path to the preferences file. If you remember from the previous article Harpoon has an init function for initialisation, where we placed that call to the setupWindow function. We’ll add a few lines of code to that:
init : function() {
Harpoon.setupWindow();
Harpoon.storePath =
air.File.applicationStorageDirectory;
Harpoon.prefsFile =
Harpoon.storePath.resolvePath("prefs.xml");
Harpoon.getPrefs();
...
},
We use the air.File.applicationStorageDirectory to obtain Harpoon’s storage directory. Then we use the resolvePath method to retrieve the path to the preferences file prefs.xml and store it as a File object. The next line, Harpoon.getPrefs(), is a call to a new function we shall write, after the break!
暂无回复
添加回复