Posted By

ryanstewart on 10/28/11


Tagged

javascript html5 phonegap lawnchair


Versions (?)

Lawnchair/jQuery Mobile Example


 / 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.

  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
Posted By: craigsf on March 18, 2012

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.

You need to login to post a comment.