从sencha touch 2中设置的字段向localstore添加文本字段值

时间:2013-02-06 10:08:18

标签: sencha-touch touch sencha-touch-2 local-storage

我是sencha touch的新手,我在下面的代码中得到错误是标签更改功能 errorUncaught TypeError: Object [object Object] has no method 'getValues'

                           var uswrfeild =  Ext.getCmp('User_details');
                                //var fieldset = Ext.create('FieldSet_PersonalSettings');
                                //var fieldset= this.tab;
                                var values = uswrfeild.getValues();
                                cart = Ext.create('Items');
                                //alert(values);
                                //cart.add({field1:'value1',field2:'value2'});
                                cart.add(values);
                                cart.sync();

下面的代码是字段集代码

                    {
                        xtype: 'container',
                        title: 'User',
                        id: 'User_details',
                        itemId: 'mycontainer3',
                        scrollable: 'vertical',
                        items: [
                            {
                                xtype: 'fieldset',
                                id: 'FieldSet_PersonalSettings',
                                itemId: 'myfieldset12',
                                margin: '2%',
                                title: 'User',
                                items: [
                                    {
                                        xtype: 'textfield',
                                        label: 'Name',
                                        name: 'name',
                                        maxLength: 31,
                                        placeHolder: 'given-name family-name'
                                    },
                                    {
                                        xtype: 'emailfield',
                                        label: 'Email',
                                        name: 'email',
                                        required: true,
                                        maxLength: 63,
                                        placeHolder: 'name@example.com'
                                    },
                                    {
                                        xtype: 'textfield',
                                        label: 'Street',
                                        name: 'street',
                                        required: true,
                                        placeHolder: 'streetname'
                                    },
                                    {
                                        xtype: 'textfield',
                                        label: 'House Number',
                                        required: true,
                                        placeHolder: '123'
                                    },
                                    {
                                        xtype: 'textfield',
                                        label: 'Zipcode',
                                        required: true,
                                        autoCapitalize: true,
                                        maxLength: 10,
                                        placeHolder: '1234AA'
                                    },
                                    {
                                        xtype: 'textfield',
                                        label: 'Country',
                                        required: true,
                                        placeHolder: 'NL'
                                    }
                                ]
                            },
                            {
                                xtype: 'fieldset',
                                margin: '2%',
                                title: 'Sharing information',
                                items: [
                                    {
                                        xtype: 'checkboxfield',
                                        label: 'Receive email',
                                        labelWidth: '75%',
                                        checked: true
                                    },
                                    {
                                        xtype: 'checkboxfield',
                                        height: 49,
                                        label: 'Upload statistics (anonymously)',
                                        labelWidth: '75%',
                                        checked: true
                                    }
                                ]
                            }

belo代码是模型

Ext.define('iFP.model.item', {
    extend: 'Ext.data.Model',

    config: {
        fields: [
            {
                name: 'name',
                type: 'string'
            },
            {
                name: 'email',
                type: 'string'
            },
            {
                name: 'street',
                type: 'string'
            },
            {
                name: 'hno',
                type: 'auto'
            },
            {
                name: 'zipcode',
                type: 'int'
            },
            {
                name: 'country',
                type: 'string'
            }
        ]
    }
});

以下代码是商店

        Ext.define('iFP.store.userlocalsettings', {
    extend: 'Ext.data.Store',

    requires: [
        'iFP.model.item'
    ],

    config: {
        autoSync: false,
        model: 'iFP.model.item',
        storeId: 'usersettingslocalstore',
        proxy: {
            type: 'localstorage'
        }
    }
});

我的目标是将文本feid值存储在浏览器localstore中。 请帮助我任何好友。 提前谢谢。

2 个答案:

答案 0 :(得分:0)

您应首先为每个字段设置ID,例如

 {
      xtype: 'textfield',
      label: 'Name',
      name: 'name',
      maxLength: 31,
      placeHolder: 'given-name family-name',
      itemId: 'tfName'
 },

然后你可以在函数

中的某个事件之后获得字段的值
var namefield = this.down('#tfName'); 
var namevalue = namefield.getValue();

...

然后您可以将该值添加到商店

var store = Ext.getStore('userlocalsettings');
store.add({name: namevalue});
store.sync();

希望这会有所帮助。

答案 1 :(得分:0)

您应该为表单设置ID。

config: {
        itemId: 'form1'
}

然后在您的控制器中添加对该ID的引用

refs: {
      myPanel: '#form1'
}

我看不到你的按钮监听器。假设监听器的名称是“onTapSave”,那么你应该试试这个,

onTapSave: function(component,button, options){

    var uswrfeild = this.getMyPanel();
    var values = uswrfeild.getValues();
}