1

我在 extjs4 MVC 结构中工作,我一直在 extjs4 中遇到问题,所有字段都提交到服务器端,但我只想将模型类的一些文件发送到服务器端。我怎样才能得到这个输出?

1)这是我的模型课

Ext.define('ab.model.sn.UserModel',{
    extend: 'Ext.data.Model',
    //idproperty:'userId',//fields property first position pk. 
    fields: ['userId','firstName','middleName','lastName','languageId','primaryEmail','birthDate','password','securityQuestionId','securityQuestionAnswer','isMale','creationTime','ipAddress','confirmationCode','userStatusId',],
    proxy:
    {
        type:'ajax',
            //type:'localstorage',
            //id:'users',
        api:
        {
            read:'http://localhost/balaee/Balaee/index.php/SocialNetworking/user/AuthenticateLogin',
            create:'http://localhost/balaee/Balaee/index.php/SocialNetworking/user/AuthenticateLogin122',
            update:'http://localhost/balaee/Balaee/index.php/SocialNetworking/user/AuthenticateLogin123'
        },//end of api
        reader:
        {
            type:'json',
        },//end of reader
        writer:
        {
            type:'json',
            root:'records',
        },//End of writer

    }//end of proxy
});

2)这是我的一些控制器文件代码

var userObject = Ext.ModelManager.create(
        {
            firstName:record.get('firstName'),
            password:record.get('password'),

        },'ab.model.sn.UserModel');


userObject.save({    
    success: function(record, operation) 
    {
        console.log("registration successssssssssss  "+record.get('userId'));
    },//End of success function
    failure: function(record, operation) 
    {
        console.log("Inside failure functionnnnn");
    },//End of failure function
    callback: function(record, operation)
    {
        console.log("Inside callback functionnnnnn");   
    }//End of callback function
});// End of check save function

3) 数据将以 json 格式传输

{"records":{"userId":"","firstName":"ram","middleName":"","lastName":"","languageId":"","primaryEmail":"","birthDate":"","password":"sham","securityQuestionId":"","securityQuestionAnswer":"","isMale":"","creationTime":"","ipAddress":"","confirmationCode":"","userStatusId":"","id":null}}

4)但我只想发送名字和密码。我不想发送所有字段。如何将数据发送到服务器端。

我想要这种格式的json

{"records":{"firstName":"ram","password":"sham"}}

请给我一些建议....

4

2 回答 2

2

你只需要覆盖 writer 的 getRecordData 函数。像这样。

 writer:
            {
                type:'json',
                root:'records',
                getRecordData: function (record) { return {"firstName" :record.data.firstName,"password": record.data.password}; },
            },
于 2013-02-25T13:01:19.523 回答
1

nscrob 的答案编码较少,因此可能是首选,但模型上还有一个内置配置persist: false. 它防止模型字段被发送到服务器端。

恕我直言,模型配置似乎并没有像在 sencha 示例中那样被使用(或者可能是因为它们没有在 sencha 示例中使用)。我认为如果您在模型中定义数据类型而不是让客户端解决它,它还可以节省少量资源,例如:

Ext.define('ab.model.sn.UserModel',{
    extend: 'Ext.data.Model',
    //idproperty:'userId',//fields property first position pk.

    // using field type definitions and explicit persistance
    fields: [
        {name: 'userId',                type: 'int',    persist: false},
        {name: 'firstName',             type: 'string'},
        {name: 'middleName',            type: 'string', persist: false},
        {name: 'lastName',              type: 'string', persist: false},
        {name: 'languageId',            type: 'int',    persist: false},
        {name: 'primaryEmail',          type: 'string', persist: false},
        {name: 'birthDate',             type: 'date',   dateFormat: 'c', persist: false},
        {name: 'password',              type: 'string'},
        {name: 'securityQuestionId',    type: 'int',    persist: false},
        {name: 'securityQuestionAnswer', type: 'string',persist: false},
        {name: 'isMale',                type: 'bool',   persist: false},
        {name: 'creationTime',          type: 'date',   dateFormat: 'c', persist: false},
        {name: 'ipAddress',             type: 'string', persist: false},
        {name: 'confirmationCode',      type: 'string', persist: false},
        {name: 'userStatusId',          type: 'int',    persist: false}
    ],
    proxy:
    {
        type:'ajax',
            //type:'localstorage',
            //id:'users',
        api:
        {
            read:'http://localhost/balaee/Balaee/index.php/SocialNetworking/user/AuthenticateLogin',
            create:'http://localhost/balaee/Balaee/index.php/SocialNetworking/user/AuthenticateLogin122',
            update:'http://localhost/balaee/Balaee/index.php/SocialNetworking/user/AuthenticateLogin123'
        },//end of api
        reader:
        {
            type:'json',
        },//end of reader
        writer:
        {
            type:'json',
            root:'records',
        },//End of writer

    }//end of proxy
});

就像我说的,还有更多的编码,但我想我会把它作为这个场景的内置处理(而不是覆盖)扔掉。如果需要,您也可以删除字段类型定义,它们不是定义persist属性所必需的。

于 2013-02-25T18:03:22.177 回答