PhoneGap: Local Storage

  • 5,131 views
Uploaded on

PhoneGap: Local Storage …

PhoneGap: Local Storage

This presentation has been developed in the context of the Mobile Applications Development course, DISIM, University of L'Aquila (Italy), Spring 2013.

http://www.ivanomalavolta.com

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
5,131
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
159
Comments
0
Likes
3

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. •––•••
  • 2.
  • 3. localStoragewindow.localStorage.setItem(“name”, “Ivano”);var name = window.localStorage.getItem(“name”);
  • 4. .key(0).getItem(“key”).setItem(“key”, “value”).removeItem(“key”).clear()
  • 5. 
  • 6. // simple class declarationfunction Person(name, surname) {this.name = name;this.surname = surname;}// object creationvar user = new Person(„Ivano‟, „Malavolta‟);// object serializationwindow.localStorage.setItem(“user”, JSON.stringify(user));// object retrievalvar current =JSON.parse(window.localStorage.getItem(“user”));
  • 7. if (window.localStorage.getItem(“user”)) {// there is an object in user} else {// the user key does not have any value}
  • 8. var users = [...]; // array of Person objectswindow.localStorage.setItem(“users”,JSON.stringify(users));var allUsers =JSON.parse(window.localStorage.getItem(“users”));var ivanos = [];for(var i=0; i<allUsers.length; i++) {if(allUsers[i].name == „Ivano‟)ivanos.push(allUsers[i]);}
  • 9. var usersNumber =JSON.parse(window.localStorage.getItem(“users“)).length;
  • 10.
  • 11. tx.executeSql("SELECT * FROM User“, [],function(tx, result) {// callback code});
  • 12. var db =openDatabase(„Test, „1.0, „Test DB, 100000);
  • 13. openDatabase(name, version, displayname, size);
  • 14. db.changeVersion("1.0", "1.1");
  • 15. db.transaction(queries, error, success);
  • 16. function populateDB(tx) {tx.executeSql(DROP TABLE IF EXISTS USER);tx.executeSql(CREATE TABLE IF NOT EXISTS USER (idunique, name, surname));tx.executeSql(INSERT INTO USER(id, name, surname)VALUES (1, ?, ?)„, [“Ivano“, “Malavolta“],success, error);}
  • 17. rows
  • 18. ...tx.executeSql(INSERT INTO USER(id, name,surname) VALUES (5,?, ?)„, [“Ivano“, “Malavolta“], success, error);}function success(tx, results) {var affected = results.rowsAffected(); // 1}function error(err) {// code for managing the error}
  • 19. lengthitem(index)
  • 20. ...tx.executeSql(„SELECT * FROM USER„, [], success, error);}function success(tx, results) {var size = results.rows.length;for (var i=0; i<size; i++){console.log(results.rows.item(i).name);}}
  • 21. ••
  • 22. ...tx.executeSql(„SELECT * FROM USER„,[], success, error);}function error(err) {console.log(err.code);}
  • 23.
  • 24.
  • 25. –––––
  • 26. requestFileSystem(type, size, successCb, [errorCb])
  • 27.
  • 28. window.requestFileSystem(LocalFileSystem.PERSISTENT,0,onSuccess,onError);function onSuccess(fileSystem) {console.log(fileSystem.name);}
  • 29. window.resolveLocalFileSystemURI("file:///userImg.png", onSuccess, onError);function onSuccess(fileEntry) {console.log(fileEntry.name);}
  • 30. The real objectsDescriptorWriting & Reading objects
  • 31.
  • 32. entry.file(win, fail);function win(file) {var reader = new FileReader();reader.onloadend = function(evt) {console.log(evt.target.result);};reader.readAsText(file);// reader.abort();};function fail(evt) {console.log(error.code);};
  • 33. append
  • 34. entry.createWriter(win, fail);function win(writer) {writer.onwrite = function(evt) {console.log(“ok");};writer.write(“Ivano Malavolta");};function fail(evt) {// error management};
  • 35. var directoryReader = dirEntry.createReader();directoryReader.readEntries(success, fail);function success(entries) {var i;for (i=0; i<entries.length; i++) {console.log(entries[i].name);}}function fail(error) {console.log(error.code);}
  • 36. window.requestFileSystem(window.PERSISTENT, 0, initFS, error);function initFS(fs) {fs.root.getFile(„log.txt, {}, win, error);}function win(fileEntry) {fileEntry.file(read, error);}function read(file) {var reader = new FileReader();reader.onloadend = function(e) {console.log(this.result);};reader.readAsText(file);}function error(err) { console.log(err);}
  • 37. • •• ••
  • 38. • • • • •
  • 39. • • • • • •
  • 40. • •••
  • 41. 