I've re-written the "first app" of the official Titanium Documentation (http://docs.appcelerator.com/titanium/latest/#!/guide/Creating_Your_First_Titanium_App), but i don't understand why the new books that i added, are not visualized if i restart the app.
It's not book.save
method that saves the new models?
From the official documentation about book.save
method
Add the book to the collection and persist it to the database.
UPDATE: here is my code. It's the same of the official documentation. This is index.js
var myBooks = Alloy.Collections.books;
var book = Alloy.createModel('books', {
title : 'Great Expectations',
author: 'Charles Dickens'
});
myBooks.add(book);
book.save();
function showBook (event){
var selectedBook = event.source;
var args = {
title: selectedBook.title,
author: selectedBook.author
};
var bookview = Alloy.createController("bookdetails", args).getView();
bookview.open();
}
$.index.open();
This is index.xml
<Alloy>
<Collection src="books"/>
<Window class="container">
<!-- Add TableView and TableViewRow -->
<TableView dataCollection="books">
<TableViewRow title="{title}" author="{author}" onClick="showBook" ></TableViewRow>
</TableView>
<Menu id="menu" platform="android">
<MenuItem id="addBook" title="Aggiungi un libro" onClick="addBook" showAsAction="Ti.Android.SHOW_AS_ACTION_IF_ROOM" />
</Menu>
</Window>
There is also bookview.js
but it is to view the details of the book.
I see one issue and one concern with your code:
Issue: your index.js is missing a fetch statement, try to add the following line in your index.js after
$.index.open();
myBooks.fetch();
fetch will grab data from the persistent storage. If you add this line in your index.js it will populate your table when the window is opened.
Concern: You have not added your code that will add new books. Your add new book code should contain a collection.add() and a model.save() type of line. This will add the new book to the current collection displayed and the book will be shown in the list. It will also save the book to persistent storage, so it will be stored on your device.
Mark