在Sencha-touch上的切换字段中设置值

时间:2014-10-04 16:46:40

标签: javascript extjs sencha-touch

我正在尝试在sencha中保存toggle-field的值并将此值发送到服务器,现在我正在监听该事件,但它无效...

我的代码是:

   onHeaderCompleteDelivery: function (button, oldValue, newValue) {
    console.log('change fired');
    if(oldValue == 0 && newValue == 1){
        console.log(oldValue);
        this.getCmp('headerCompleteDelivery').setValue(oldValue);
    }
    else {
        console.log(oldValue);
        this.getCmp('headerCompleteDelivery').setValue(oldValue);
    }
},

我做错了什么?

谢谢!

2 个答案:

答案 0 :(得分:0)

我并不完全明白你要完成什么,但是看看代码,你的if和else分支都执行完全相同的代码,这可能就是问题。

此外,看起来oldValue和newValue参数在change事件中被反转:newValue参数位于oldValue参数之前(参见http://docs.sencha.com/touch/2.3.1/#!/api/Ext.field.Toggle-event-change)。

答案 1 :(得分:0)

里卡多提到,很难说出你在做什么:

  • 看起来像是您正在收听的切换字段的更改事件
  • 但似乎不是标准事件,因为newValue是第二个参数而oldValue是第三个
  • 您可以使用按钮命名触发组件,这可能表示您正在收听按钮,但该按钮不具有切换字段的值

这将让你无处可以


如果这确实是切换字段,请重写您的代码,以保持简单

  • 我已将您要设置的值更改为0和1,以显示当时将设置的内容。
  • 我删除了第二个if子句,因为很明显它是相反的值。
  • 我解释了每个值正在做什么,因为你在参数
  • 中的错误位置有newValue
  • 如果这里没有意义的话。我添加了一个简短的表格

/**
 * togglefield change event
 *
 * @params {object} togglefield
 * @params {number} newValue    the value after the change
 * @params {number} oldValue    the value before the change
 */
 onToggelNameChange: function (togglefield, newValue, oldValue) {
    console.log('Togglefield ' + togglefiled.getName() + ' fired change event');
    console.log('Togglefield is now set to: ' + !(!newValue));
    // what is the scope of "this"? you better be sure
    this.getCmp('headerCompleteDelivery').setValue(newValue);

    // or you can try this approach. But make sure header has a value config.
    var header = Ext.Viewport.down('#headerCompleteDelivery');
    header.setValue(newValue);
},

你应该采用不同的方法。

  • 使用表格
  • 将toggelfield放入表单并为字段添加名称
  • 点击提交按钮后,从表单(form.getValues())
  • 中获取值
  • 在值中,您将拥有togglefield的当前值

这里有一个例子:

查看

Ext.define('App.view.Main', {
    extend: 'Ext.Container',
    xtype: 'main',

    config: {

        items: [
            {
                xtype: 'form',
                items: [
                    {
                        xtype: 'textfield',
                        name: 'name',
                    },
                    {
                        xtype: 'passwordfield',
                        name: 'password'
                    },
                    {
                        xtype: 'togglefield',
                        name: 'isActiveToggle',
                        label: 'Test Toggle Field'
                    },
                    {
                        xtype: 'button',
                        itemId: 'btnSubmit',
                        text: 'Submit'
                    }
                ]
            }
        ]
    }
});

控制器

Ext.define('App.controller.Main', {
    extend: 'Ext.app.Controller',

    config: {
        refs: {
            submitBtn: '#btnSubmit',
        },
        control: {
            submitBtn: { tap: 'onSubmitBtnTap'},
        }
    },

    onSubmitBtnTap: function (btn) {
        var form = btn.up('.form'),
            values = form.getValues();

        // here you will get an Object with
        {
            name: 'Value of name field',
            password: 'Value of password field',
            isActiveToggle: true // <== this is the value you want to send
        }

        this.sendDataToServer(values.isActiveToggle); // your Ajax call with toggleValue
    }
});