【问题标题】:Titanium Appcelerator - Model to local sqlite dB undefined valuesTitanium Appcelerator - 模型到本地 sqlite dB 未定义值
【发布时间】:2014-05-31 05:03:13
【问题描述】:

我正在尝试将本地 sqlite dB(我已经测试过并且值在那里)中的值加载到我可以在我的视图中使用的全局模型中。当我在使用Ti.API.info(library.at(i));in index.js 创建模型后尝试打印模型的值时,它大部分时间返回未定义,有时还会返回像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');

【问题讨论】:

    标签: javascript sqlite model titanium appcelerator


    【解决方案1】:

    问题在于你的 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());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多