/ 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
Copy this code and paste it in your HTML
<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>