繁体   English   中英

如何从钛合金数据库中检索值?

[英]How to retrive values from Database in Titanium Alloy?

我是Titanium API的新手。 我想知道如何从数据库检索值并在UI中显示。 我创建了一个模型并插入了一行。 我的代码如下

模型:

var moment = require('alloy/moment');

exports.definition = {
    config : {
            "columns": {
            "id":"text",
            "LanguageName":"text"
            },
            "adapter": {
                "type": "sql",
                "collection_name": "UserLanguage"
            }
    },

    extendModel: function(Model) {      
        _.extend(Model.prototype, {

        });

        return Model;
    },

    extendCollection: function(Collection) {        
        _.extend(Collection.prototype, {

        });

        return Collection;
    }
};

视图:

<Alloy>
    <Window id="changeLangWindow" title="Plan India">
        <Picker id="langPicker">
            <PickerRow title="Select a Language"/>
            <PickerRow title="English"/>
            <PickerRow title="French"/>
            <PickerRow title="Spanish"/>
        </Picker>
        <Button class="button" onClick="saveLang">Proceed</Button>
        <Label class="question">Selected Language is: -------</Label>
    </Window>
</Alloy>

控制器:

function saveLang() {

    var UserLang = Alloy.Collections.UserLanguage;

    // Create a new model for the todo collection
    var task = Alloy.createModel('UserLanguage', {
        id : '1',
        LanguageName : 'English'
    });

    // add new model to the global collection
      UserLang.add(task);

    // save the model to persistent storage
    task.save();

    // reload the tasks
     UserLang.fetch();

}

我想从“ UserLanguage”模型中选择“ LanguageName”的值,并显示在XML文件的视图中。

有建议的任何建议请!

//尝试使用此代码可能对您有所帮助。

if (Alloy.Collections.UserLanguage.length) {
     Alloy.Collections.UserLanguage.map(function(obj) {

        Ti.API.Log(" LanguageName "+ obj.get('LanguageName'));
        Ti.API.Log(" LanguageName "+ obj.id);

     });

}

谢谢,

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM