gpt4 book ai didi

javascript - Titanium Appcelerator - 模型到本地 sqlite dB 未定义值

转载 作者:行者123 更新时间:2023-12-03 12:30:11 25 4
gpt4 key购买 nike

我正在尝试将本地 sqlite dB 中的值(我已经测试过并且值在那里)加载到我可以在 View 中使用的全局模型中。当我在 index.js 中使用 Ti.API.info(library.at(i)); 创建模型后尝试打印模型的值时,大多数时候它会返回 undefined,有时会返回函数调用如 function lastIndexOf() { [native code] }。这是怎么回事?我该如何解决?这是我的模型 (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;
}

};

以下是我如何将其读入模型(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());
}

关于javascript - Titanium Appcelerator - 模型到本地 sqlite dB 未定义值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23966551/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com