如何在extjs4中将模型特定属性发送到服务器端?

时间:2013-02-25 11:29:28

标签: json extjs extjs4 extjs-mvc

我在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)但我只想发送firstName和password.I不想发送所有字段。如何将数据发送到服务器端。

我希望json采用这种格式

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

请给我一些建议......

2 个答案:

答案 0 :(得分:2)

您只需要覆盖编写器的getRecordData函数。像这样。

 writer:
            {
                type:'json',
                root:'records',
                getRecordData: function (record) { return {"firstName" :record.data.firstName,"password": record.data.password}; },
            },

答案 1 :(得分:1)

nscrob的答案编码较少,因此可能是首选,但模型上还有built-in configpersist: 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属性。