钛合金Appcelerator -模型到本地sqlite数据库未定义的值

Titanium Appcelerator - Model to local sqlite dB undefined values

本文关键字:数据库 sqlite 未定义 Appcelerator 模型 钛合金      更新时间:2023-09-26

我正试图从本地sqlite dB(我已经测试和值在那里)加载值到一个全局模型,我可以在我的视图中使用。当我尝试在index.js中使用Ti.API.info(library.at(i));创建模型后打印模型的值时,它在大多数时间返回未定义,有时像function lastIndexOf() { [native code] }这样的函数调用。发生了什么,我该如何解决?这是我的模型(upcominggrace .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;
}

};

这是我是如何读取它到一个模型(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);
}

我在alloy.js文件中也有这个

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

问题是你的for循环:

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

不需要调用library.at(i),只使用i元素。所以正确的代码应该是:

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