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

时间:2014-09-22 11:33:18

标签: javascript titanium titanium-mobile titanium-alloy titanium-modules

我是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文件的视图中。

请提供任何解释建议!

1 个答案:

答案 0 :(得分:2)

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

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

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

     });

}

谢谢,