1

我试图用model.save()POST 一个新用户。但是我检查了request payload,发现它不仅发送了数据,还发送了模型的其他部分。这使我的服务器无法解析有效负载。

生成的请求有效负载:

{"phantom":true,"internalId":"ext-record-58","raw":{},"data":{"userId":0,"userName":"Amy"},"modified":{"userName":""},"hasListeners":{},"events":{},"stores":[],"dirty":true,"id":"AM.model.User-ext-record-58"}

但所需的请求负载应该是:

{"userId":0,"userName":"Amy"}

而且我知道在我打电话之前我的模型的“幻影”是错误的model.save()。但它在请求有效负载中变得真实。这是一个线索吗?

模型:

 Ext.define('AM.model.User',{
        extend: 'Ext.data.Model',
        fields: [
            { name: 'userId', type: 'int' },
            { name: 'userName', type: 'string' },
            { name: 'createdTime', type: 'string' },

        ],
        idProperty: 'userId',
        associations: [
        {
            type: 'hasOne',
            model: 'AM.model.ModelA',
            name:'modelA',
            associationKey:'modelA',
            getterName:'modelA'
        },
        {
            type: 'hasOne',
            model: 'AM.model.ModelB',
            name:'modelB',
            associationKey:'modelB',
            getterName:'modelB'
        }
        ],
        proxy: {
            type: 'rest',
            success:true,
            url:'../restful/users',
            writer:{
                type:'json',
                getRecordData:function(record){ //parse createdTime to the format Y-m-d
                    record.set('createdTime', Ext.Date.format(new Date(record.get('createdTime')), "Y-m-d"));
                    return record;
                }   
            },
            reader: {
                type: 'json'
            }
        }
    });

这是要发布数据的视图。视图会将数据填充到模型中:

Ext.define('AM.view.UserRegisterForm',{
    extend:'Ext.form.Panel.',
    alias:'widget.userRegisterForm',
    fields:new Array(), //I want to render the fields in xtemplate, so instead of adding the fields to items, I use an array to manage them.

    retrieveData(model){
        model.set('userName', this.fields[0].getValue());
        model.set('createdTime',this.fields[1].getValue());
    }
}

控制器中发送 POST 请求的函数:

postUser:function(){
    var userRegisterForm= this.getUserRegisterForm();
    var userModel = this.getUserModel();
    var user= new userModel();
    var me = this;

    userRegisterForm.retrieveFieldData(user);
    console.log(user); //the data in console looks fine!
    user.save({
        success: function(response) {
            //do something...
        },failure:function(response) {
            alert('fail');
        }
    });
}
4

1 回答 1

3

当您覆盖Where 时,您将返回完整记录getRecordData,因为您只是要返回记录数据record.getData()

一些额外的建议。不要覆盖 getRecordData 来设置模型创建日期。如果不存在,请使用模型 defaultValue 属性为其分配一个新日期。

于 2012-10-08T13:28:03.367 回答