【问题标题】:How to send model specific attributes to server side in extjs4?如何在 extjs4 中将模型特定属性发送到服务器端?
【发布时间】:2013-02-25 11:29:27
【问题描述】:

我在 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"}}

请给我一些建议....

【问题讨论】:

    标签: json extjs extjs4 extjs-mvc


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      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 属性所必需的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-16
        • 2019-11-22
        • 1970-01-01
        • 2020-08-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多