• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
PhoneGap: Local Storage
 

PhoneGap: Local Storage

on

  • 4,535 views

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

Statistics

Views

Total Views
4,535
Views on SlideShare
4,352
Embed Views
183

Actions

Likes
3
Downloads
133
Comments
0

1 Embed 183

http://www.ivanomalavolta.com 183

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    PhoneGap: Local Storage PhoneGap: Local Storage Presentation Transcript

    • •––•••
    • localStoragewindow.localStorage.setItem(“name”, “Ivano”);var name = window.localStorage.getItem(“name”);
    • .key(0).getItem(“key”).setItem(“key”, “value”).removeItem(“key”).clear()
    • 
    • // 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”));
    • if (window.localStorage.getItem(“user”)) {// there is an object in user} else {// the user key does not have any value}
    • 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]);}
    • var usersNumber =JSON.parse(window.localStorage.getItem(“users“)).length;
    • tx.executeSql("SELECT * FROM User“, [],function(tx, result) {// callback code});
    • var db =openDatabase(„Test, „1.0, „Test DB, 100000);
    • openDatabase(name, version, displayname, size);
    • db.changeVersion("1.0", "1.1");
    • db.transaction(queries, error, success);
    • 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);}
    • rows
    • ...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}
    • lengthitem(index)
    • ...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);}}
    • ••
    • ...tx.executeSql(„SELECT * FROM USER„,[], success, error);}function error(err) {console.log(err.code);}
    • –––––
    • requestFileSystem(type, size, successCb, [errorCb])
    • window.requestFileSystem(LocalFileSystem.PERSISTENT,0,onSuccess,onError);function onSuccess(fileSystem) {console.log(fileSystem.name);}
    • window.resolveLocalFileSystemURI("file:///userImg.png", onSuccess, onError);function onSuccess(fileEntry) {console.log(fileEntry.name);}
    • The real objectsDescriptorWriting & Reading objects
    • 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);};
    • append
    • entry.createWriter(win, fail);function win(writer) {writer.onwrite = function(evt) {console.log(“ok");};writer.write(“Ivano Malavolta");};function fail(evt) {// error management};
    • 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);}
    • 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);}
    • • •• ••
    • • • • • •
    • • • • • • •
    • • •••
    • 