phonegap: local storage

67

Upload: gran-sasso-science-institute

Post on 20-Aug-2015

6.580 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: PhoneGap: Local Storage
Page 2: PhoneGap: Local Storage
Page 3: PhoneGap: Local Storage

Page 4: PhoneGap: Local Storage
Page 5: PhoneGap: Local Storage
Page 6: PhoneGap: Local Storage

Page 7: PhoneGap: Local Storage
Page 8: PhoneGap: Local Storage
Page 9: PhoneGap: Local Storage

localStorage

window.localStorage.setItem(“name”, “Ivano”);

var name = window.localStorage.getItem(“name”);

Page 10: PhoneGap: Local Storage

.key(0)

.getItem(“key”)

.setItem(“key”, “value”)

.removeItem(“key”)

.clear()

Page 11: PhoneGap: Local Storage
Page 12: PhoneGap: Local Storage

// simple class declaration

function Person(name, surname) {

this.name = name;

this.surname = surname;

}

// object creation

var user = new Person(„Ivano‟, „Malavolta‟);

// object serialization

window.localStorage.setItem(“user”, JSON.stringify(user));

// object retrieval

var current = JSON.parse(window.localStorage.getItem(“user”));

Page 13: PhoneGap: Local Storage

if (window.localStorage.getItem(“user”)) {

// there is an object in user

} else {

// the user key does not have any value

}

Page 14: PhoneGap: Local Storage

var users = [...]; // array of Person objects

window.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]);

}

Page 15: PhoneGap: Local Storage

var usersNumber =

JSON.parse(window.localStorage.getItem(“users“)).length;

Page 16: PhoneGap: Local Storage
Page 17: PhoneGap: Local Storage
Page 18: PhoneGap: Local Storage

tx.executeSql("SELECT * FROM User“, [],

function(tx, result) {

// callback code

});

Page 19: PhoneGap: Local Storage

var db =

openDatabase(„Test', „1.0', „Test DB', 100000);

Page 20: PhoneGap: Local Storage

openDatabase(name, version, displayname, size);

Page 21: PhoneGap: Local Storage

db.changeVersion("1.0", "1.1");

Page 22: PhoneGap: Local Storage

db.transaction(queries, error, success);

Page 23: PhoneGap: Local Storage
Page 24: PhoneGap: Local Storage

function populateDB(tx) {

tx.executeSql('DROP TABLE IF EXISTS USER');

tx.executeSql('CREATE TABLE IF NOT EXISTS USER (id

unique, name, surname)');

tx.executeSql('INSERT INTO USER(id, name, surname)

VALUES (1, ?, ?)„, [“Ivano“, “Malavolta“],

success, error);

}

Page 25: PhoneGap: Local Storage

rows

Page 26: PhoneGap: Local Storage

...

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

}

Page 27: PhoneGap: Local Storage

length

item(index)

Page 28: PhoneGap: Local Storage

...

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);

}

}

Page 29: PhoneGap: Local Storage

Page 30: PhoneGap: Local Storage

...

tx.executeSql(„SELECT * FROM USER„,[], success, error);

}

function error(err) {

console.log(err.code);

}

Page 31: PhoneGap: Local Storage
Page 32: PhoneGap: Local Storage
Page 33: PhoneGap: Local Storage
Page 34: PhoneGap: Local Storage
Page 35: PhoneGap: Local Storage
Page 36: PhoneGap: Local Storage
Page 37: PhoneGap: Local Storage

Page 38: PhoneGap: Local Storage

requestFileSystem(type, size, successCb, [errorCb])

Page 39: PhoneGap: Local Storage
Page 40: PhoneGap: Local Storage

window.requestFileSystem(

LocalFileSystem.PERSISTENT,

0,

onSuccess,

onError);

function onSuccess(fileSystem) {

console.log(fileSystem.name);

}

Page 41: PhoneGap: Local Storage
Page 42: PhoneGap: Local Storage

window.resolveLocalFileSystemURI(

"file:///userImg.png", onSuccess, onError);

function onSuccess(fileEntry) {

console.log(fileEntry.name);

}

Page 43: PhoneGap: Local Storage

The real objects

Descriptor

Writing & Reading objects

Page 44: PhoneGap: Local Storage
Page 45: PhoneGap: Local Storage
Page 46: PhoneGap: Local Storage
Page 47: PhoneGap: Local Storage
Page 48: PhoneGap: Local Storage
Page 49: PhoneGap: Local Storage
Page 50: PhoneGap: Local Storage
Page 51: PhoneGap: Local Storage
Page 52: PhoneGap: Local Storage
Page 53: PhoneGap: Local Storage

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);

};

Page 54: PhoneGap: Local Storage
Page 55: PhoneGap: Local Storage

append

Page 56: PhoneGap: Local Storage
Page 57: PhoneGap: Local Storage

entry.createWriter(win, fail);

function win(writer) {

writer.onwrite = function(evt) {

console.log(“ok");

};

writer.write(“Ivano Malavolta");

};

function fail(evt) {

// error management

};

Page 58: PhoneGap: Local Storage
Page 59: PhoneGap: Local Storage

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);

}

Page 60: PhoneGap: Local Storage

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);}

Page 61: PhoneGap: Local Storage
Page 62: PhoneGap: Local Storage

Page 63: PhoneGap: Local Storage

Page 64: PhoneGap: Local Storage

Page 65: PhoneGap: Local Storage

Page 66: PhoneGap: Local Storage
Page 67: PhoneGap: Local Storage