The AppStore data store can be used just as any other data store that conforms to the dojo.data APIs. However, the AppStore data store does require a working APP feed for the write functions to work. The following example application demonstrates the Read and Identity API functions of the AppStore data store.
Here is the feed to use in these demonstrations; it is accessible in this documentation package.
<?xml version='1.0' encoding='utf-8'?> <feed xmlns='http://www.w3.org/2005/Atom' xml:lang='en-US'> <title>Example.com</title> <link rel="alternate" type="text/html" href="http://example.com/" hreflang="en" title="Example.com" /> <subtitle type='text'>Example.com's Sample Feed</subtitle> <rights>Copyright Example.com</rights> <id>http://example.com/samplefeed.xml</id> <updated>2007-08-07T20:00:00-05:00</updated> <link rel="self" type="application/atom+xml" href="http://www.example.com/samplefeed.xml"/> <entry> <title>Test Entry #1</title> <id>http://example.com/samplefeed.xml/entry/1</id> <link rel='alternate' href='http://example.com/1.html'/> <summary type='html'> <p>This is a sample entry in our Atom feed. It is simply a large paragraph in the summary.<p> <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras cursus. Aliquam eget metus sed leo lacinia rutrum. Nunc lacus lacus, viverra placerat, laoreet nec, placerat vel, eros. Donec nec magna id sem commodo rutrum. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Aenean pede. Quisque vel leo. In vitae nisi. Curabitur sodales congue nibh. Maecenas ultrices ante nec ipsum. Aenean quis nibh. Aenean semper, quam vitae ullamcorper euismod, arcu leo tincidunt nunc, vel pulvinar turpis dolor a elit. Praesent nonummy nunc faucibus nibh. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus laoreet, ante auctor condimentum venenatis, magna quam varius elit, at feugiat dolor metus id quam. Etiam enim.<p> </summary> <author> <name>Test User</name> <email>test@example.com</email> </author> <updated>2007-08-07T04:00:00-05:00</updated> </entry> <entry> <title>Test Entry #2</title> <id>http://example.com/samplefeed.xml/entry/2</id> <link rel='alternate' href='http://example.com/2.html'/> <summary type='text'> This is a sample entry in our Atom feed. It is simply a large paragraph in the summary. This is straight text. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Cras cursus. Aliquam eget metus sed leo lacinia rutrum. Nunc lacus lacus, viverra placerat, laoreet nec, placerat vel, eros. Donec nec magna id sem commodo rutrum. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos hymenaeos. Aenean pede. Quisque vel leo. In vitae nisi. Curabitur sodales congue nibh. Maecenas ultrices ante nec ipsum. Aenean quis nibh. Aenean semper, quam vitae ullamcorper euismod, arcu leo tincidunt nunc, vel pulvinar turpis dolor a elit. Praesent nonummy nunc faucibus nibh. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Vivamus laoreet, ante auctor condimentum venenatis, magna quam varius elit, at feugiat dolor metus id quam. Etiam enim. </summary> <author> <email>test@example.com</email> </author> <updated>2007-08-07T06:00:00-05:00</updated> </entry> </feed>
Next, create an AppStore appliation in markup, declaratively, from the feed at the given URL. See the following code to create the AppStore data store:
<div dojoType="ibm_atom.data.AppStore" url="samplefeed.xml" jsId="appStore">
However, just adding this markup does not affect the page. You must include the dojo library, and explicitly load the parser, which examines the page and instantiates the widgets. This is done in the head element, with a script tag. The code looks like this:
<script type="text/javascript">djConfig = {parseOnLoad: true};</script> <script type="text/javascript" src="../../../dojo/dojo.js"></script> <script> dojo.require("dojo.parser"); dojo.require("ibm_atom.data.AppStore"); </script>
Now, the store has been instantiated and the feed is loaded. As an example of some of the more advanced tasks that can be completed with a store, next you can add in a Tree widget, which gets its data from the AppStore and display a list of titles.
<div dojoType="dijit.Tree" id="tree" store="appStore" query="{title:'*t*'}"></div>
Examining the previous markup, the Tree widget performs a query on the AppStore
for entries that have a title of * (aka, matching any title at all). This results
in the tree showing the title of each matched entry.
Examine some simple functions of the AppStore. First is fetching items from
the feed. Suppose that you want to fetch the entry with the title "Test Entry
#1". Perhaps you might run a fetch against the AppStore like this:
<script type="text/javascript"> dojo.addOnLoad(function(){ function callback(items, request){ console.debug(items.length); if(items.length > 0) console.debug(items[0].title); } appStore.fetch({query: {title: "Test Entry #1"}, onComplete: callback}); }); </script>
Having fetched an item, there are several functions you can call with it. This includes getting attributes of the item (in the previous code, these calls were not used in favor of accessing the elements of the entry object directly), checking if the item is an item of our store, and examining the identity of the item. See the following examples:
<script type="text/javascript"> dojo.addOnLoad(function() { function callback(items, request){ var item = items[0]; console.debug(appStore.getValue(item, 'summary')); console.debug(appStore.isItem(item)); console.debug(appStore.getIdentity(item)); } appStore.fetch({query: {title: "Test Entry #1"}, onComplete: callback}); }); </script>