ExtJS 4 MVC,获取Checkbox值整数而不是bool

Mat*_*hyn 2 checkbox json extjs extjs4

我正在使用表单来编辑模型.我以JSON格式接收数据.这是我的问题:我只在INT中收到Checkbox的数据:1或0,我无法更改它.JSON:

{ "checkboxValue": 1 }
Run Code Online (Sandbox Code Playgroud)

ExtJS模型中的此字段定义为INT类型.模型:

{name: "checkboxValue", type: Ext.data.Types.INT},
Run Code Online (Sandbox Code Playgroud)

然后我设置值以这种方式形成:

formCmp.loadRecord(loadedStore.getAt(0));
Run Code Online (Sandbox Code Playgroud)

并且我的复选框设置正确:当我收到1时,它被选中,0 - 未选中.

但是当我尝试保存记录并以这种方式将数据发送到服务器时:

form.updateRecord(form.getRecord());
record.save();
Run Code Online (Sandbox Code Playgroud)

我需要Checkbox也有INT值 - 1或0.但它只有BOOL值 - true或false所以当发送JSON时该值为NaN.

{
    "checkboxValue": NaN
}
Run Code Online (Sandbox Code Playgroud)

我认为,该函数.updateRecord(..)遍历所有元素,当它到达复选框时,它会尝试获取值,并且值为BOOL

有人知道如何使复选框的输出值INT?

小智 5

Ext.form.Basic.updateForm使用getFieldValues方法检索更新记录的新数据,而getFieldValues方法仅返回复选框的布尔值,而不管inputValueuncheckedValue等属性.所以我会使用转换函数为模型的字段将提供的布尔值转换为整数,方式如下:

Ext.define('MyModel', {
    extend: 'Ext.data.Model',
    fields: [
        {
            name: 'flag',
            type: 'int',
            convert: function (v, record) {
                return typeof v === 'boolean' ? (v === true ? 1 : 0) : v;
            }
        }
    ],
    ...
});
Run Code Online (Sandbox Code Playgroud)

这是一个完整的jsfiddle