Lawnchair/jQuery Mobile Example


/ Published in: JavaScript
Save to your folder(s)

A quick, simple example of using Lawnchair to store and retrieve data for a jQuery Mobile based PhoneGap project. Written by a JavaScript newbie.


Copy this code and paste it in your HTML
  1. <script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
  2. <script src="http://code.jquery.com/mobile/1.0rc1/jquery.mobile-1.0rc1.min.js"></script>
  3. <script src="scripts/phonegap.js" type="text/javascript"></script>
  4. <script src="scripts/Lawnchair.js"></script>
  5. <script src="scripts/webkit-sqlite.js"></script>
  6. <script src="scripts/dom.js"></script>
  7. <script type="text/javascript">
  8.  
  9. $(function(e) {
  10. var beers = Lawnchair({name:'beers'},function(e){
  11. console.log('storage open');
  12. });
  13. // uncomment to clear the database
  14. //beers.nuke();
  15.  
  16.  
  17. beers.all(function(arrBeers){
  18. for(var i = 0; i<arrBeers.length;i++)
  19. {
  20. console.log(arrBeers.length);
  21. var listdiv = document.createElement('li');
  22. listdiv.setAttribute('id','listdiv');
  23. listdiv.innerHTML = arrBeers[i].value.beername;
  24. $('#beer_list').append(listdiv);
  25. }
  26. $('#beer_list').listview("refresh");
  27. });
  28.  
  29. $('#save').click(function(e){
  30.  
  31. var obj1 = {beername:"Wet Hop",brewername:"Deschuttes",brewerlocation:"Bend, OR"
  32. ,beerstyle:"IPA",quantity:1,purchasedate:"12/11/2011",price:"9.00"
  33. ,cellardate:"9/11/2011",cellartemp:40,brewdate:"8/10/2011"};
  34. var obj2 = {beername:"Vertical Epic 11",brewername:"Stone",brewerlocation:"San Diego, CA"
  35. ,beerstyle:"Belgian",quantity:1,purchasedate:"1/10/2011",price:"15.00"
  36. ,cellardate:"1/12/2011",cellartemp:45,brewdate:"10/10/2010"};
  37. beers.save({key:"1",value:obj1});
  38. beers.save({key:"2",value:obj2});
  39.  
  40.  
  41. });
  42.  
  43. $('#retrieve').click(function(e){
  44. beers.get("1",function(obj){
  45. console.log(obj);
  46. });
  47. });
  48. $('#modify').click(function(e) {
  49. beers.get("1",function(thisobj){
  50. console.log(thisobj);
  51. var obj = {};
  52. obj = thisobj.value;
  53. obj.beername = "Not Wet Hop";
  54. beers.save({key:thisobj.key,value:obj});
  55. });
  56. });
  57. });
  58. </script>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.