/ Published in: JavaScript
A quick, simple example of using Lawnchair to store and retrieve data for a jQuery Mobile based PhoneGap project. Written by a JavaScript newbie.
Expand |
Embed | Plain Text
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script> <script src="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.js"></script> <script src="scripts/phonegap.js" type="text/javascript"></script> <script src="scripts/Lawnchair.js"></script> <script src="scripts/webkit-sqlite.js"></script> <script src="scripts/dom.js"></script> <script type="text/javascript"> $(function(e) { var beers = Lawnchair({name:'beers'},function(e){ console.log('storage open'); }); // uncomment to clear the database //beers.nuke(); beers.all(function(arrBeers){ for(var i = 0; i<arrBeers.length;i++) { console.log(arrBeers.length); var listdiv = document.createElement('li'); listdiv.setAttribute('id','listdiv'); listdiv.innerHTML = arrBeers[i].value.beername; $('#beer_list').append(listdiv); } $('#beer_list').listview("refresh"); }); $('#save').click(function(e){ var obj1 = {beername:"Wet Hop",brewername:"Deschuttes",brewerlocation:"Bend, OR" ,beerstyle:"IPA",quantity:1,purchasedate:"12/11/2011",price:"9.00" ,cellardate:"9/11/2011",cellartemp:40,brewdate:"8/10/2011"}; var obj2 = {beername:"Vertical Epic 11",brewername:"Stone",brewerlocation:"San Diego, CA" ,beerstyle:"Belgian",quantity:1,purchasedate:"1/10/2011",price:"15.00" ,cellardate:"1/12/2011",cellartemp:45,brewdate:"10/10/2010"}; beers.save({key:"1",value:obj1}); beers.save({key:"2",value:obj2}); }); $('#retrieve').click(function(e){ beers.get("1",function(obj){ console.log(obj); }); }); $('#modify').click(function(e) { beers.get("1",function(thisobj){ console.log(thisobj); var obj = {}; obj = thisobj.value; obj.beername = "Not Wet Hop"; beers.save({key:thisobj.key,value:obj}); }); }); }); </script>
Comments
Subscribe to comments
You need to login to post a comment.

One thing I can't seem to find anywhere is an example of using Lawnchair to retrieve an object of a certain type by key and then display one or more of its properties.
For example, what I want to find is:
store.save (key:"myobjectkey",myobject); // save the object using the lawnchair store retreivedObject = store.get("myobjectkey"); // retreive it by the key it was saved with alert(retreivedObject.property1); // display one of its properties
Why is that code not anywhere? I would think it would be the most important example of how to use lawnchair.
In your example you have a snippet doing a store.get but it doesn't do anything with the object once retrieved. In the example on the lawnchair site, same thing. They don't show how to grab an object from the store and then use it outside the store.