My app has a scoreboard page that is supposed to fetch data from sqlite db and display it onNavigatingTo page in a ListView but it does not do it as expected.
The xml page to display the results:
<Page xmlns="http://schemas.nativescript.org/tns.xsd" loaded="pageLoaded" navigatingTo="onNavigatingTo">
<ActionBar title="Scoreboard">
<NavigationButton text="Back" android.systemIcon="ic_menu_back" tap="homeTap"/>
</ActionBar>
<StackLayout orientation="vertical">
<Label text="Your Performance Sheet"></Label>
<ListView items= "{{results}}" >
<ListView.itemTemplate>
<Label text="{{testname}}"/>
<Label text="{{score}}"/>
<Label text="{{percent}}"/>
</ListView.itemTemplate>
</ListView>
</StackLayout>
</Page>
The scoreboard-view-model:
var Observable = require("data/observable").Observable;
var ObservableArray = require("data/observable-array").ObservableArray;
var Sqlite = require("nativescript-sqlite");
function scoreViewModel (database) {
var viewModel = new Observable();
viewModel.results = new ObservableArray([]);
viewModel.select = function () {
this.results = new ObservableArray([]);
database.all("SELECT * FROM scores").then(rows => {
for (var row in rows) {
this.results.push(rows[row]);
}
}, error => {
console.log("SELECT ERROR", error);
})
}
viewModel.select();
return viewModel;
}
exports.scoreViewModel = scoreViewModel;
I am selecting all the data from the scores table and pushing to viewModel.results array which is already bound to the view.
The scoreboard.js :
var observable = require("data/observable");
var scoreViewModel = require("./scoreboard-view-model").scoreViewModel;
var page;
var Sqlite = require("nativescript-sqlite");
exports.onNavigatingTo = function (args) {
page = args.object;
(new Sqlite("scoreboard.db")).then(db => {
db.execSQL("CREATE TABLE IF NOT EXISTS scores (id INTEGER PRIMARY KEY AUTOINCREMENT, testname TEXT, score TEXT, percent TEXT)")
.then(id => {
page.bindingContext = scoreViewModel(db);
}, error => {
console.log(error)
});
}, error => {
console.log(error);
});
}
Someone please help me get the data to show up in the list view.