JBaczuk
JBaczuk

Reputation: 14619

Titanium Appcelerator - Model to local sqlite dB undefined values

I am trying to load the value from the local sqlite dB (which I have tested and the values are there) into a global model that I can use in my views. When I try to print the values of the model after creating it using Ti.API.info(library.at(i));in index.js, it returns undefined most of the time and sometimes a function call like function lastIndexOf() { [native code] }. What is going on and how can I fix it? Here is my model (UpcomingRaces.js):

exports.definition = {
config: {
    columns: {
        "Name": "string",
        "Location": "string",
        "Date": "string",
        "Url": "string"//,
        //"Id" : "INTEGER PRIMARY KEY AUTOINCREMENT"
    },
    defaults: {
        "Name": "string",
        "Location": "string",
        "Date": "string",
        "Url": "string"
    },
    adapter: {
        type: "sql",
        collection_name: "UpcomingRaces",
        //idAttribute: "Id"
    }
},
extendModel: function(Model) {
    _.extend(Model.prototype, {
        // extended functions and properties go here
    });

    return Model;
},
extendCollection: function(Collection) {
    _.extend(Collection.prototype, {
        // extended functions and properties go here
        comparator: function(UpcomingRaces) {
            return UpcomingRaces.get('Name');
        }
    });

    return Collection;
}

};

Here is how I am reading it into a model (index.js):

var library = Alloy.Collections.UpcomingRaces;
library.fetch();

function prepareView()
{
// Automatically Update local DB from remote DB
updateRaces.open('GET', 'http://fakeurl.com/api/UpcomingRaces');
updateRaces.send();
library && library.fetch();
// Read DB to create current upcomingRaces model
// Insert the JSON data to the table view
for ( var i in library ) {
    Ti.API.info(library.at(i));
    data.push(Alloy.createController('row', {
        Name : library[i]['Name'],
        Date : library[i]['Date']
    }).getView());
}
$.table.setData(data);
}

Also I've got this in my alloy.js file

Alloy.Collections.UpcomingRaces = Alloy.createCollection('UpcomingRaces');

Upvotes: 0

Views: 872

Answers (1)

WeMakeSoftware
WeMakeSoftware

Reputation: 9162

the problem is with your for loop:

// Insert the JSON data to the table view
for ( var i in library ) {  // i here is an instance of the Model, not an index
    Ti.API.info(library.at(i)); // <-- error here
    data.push(Alloy.createController('row', {
        Name : library[i]['Name'],
        Date : library[i]['Date']
    }).getView());
}

no need to call library.at(i), just use i element. So the correct code would be:

// Insert the JSON data to the table view
for ( var element in library ) {
    Ti.API.info(JSON.stringify(element));
    data.push(Alloy.createController('row', {
        Name : element.get('Name'),
        Date : element.get('Date')
    }).getView());
}

Upvotes: 1

Related Questions