ExtJS:store - proxy - writer:如何在同步时始终为记录添加/设置值?

时间:2012-04-27 10:01:16

标签: extjs extjs4

经过两天的挣扎,我已经创建了自己的Ext.data.Store课程(见下面的代码)。

请注意,即使考虑了代理及其编写器的所有配置,也会忽略配置writeAllFields: true,并且仅发送更改的值(这对我来说是个大问题)。

所以,现在,为了避免这个(巨大的)问题,我想总是为发送的记录添加一个预定义的值(它总是一个值,类似于{{ 1}})。出于安全原因,我需要这个。

id_partner: 3

知道我怎么能这样做吗?

2 个答案:

答案 0 :(得分:1)

这是我的代码有效。这是一个黑客,但它适用于ExtJS 4.0.7和ExtJS 4.1 beta。

Ext.define('Ext.data.StoreHandleErrors', {
    extend: 'Ext.data.Store',
    alias: 'data.storehandleerrors',

    constructor: function(config) {
        /* (!!) proxy properties overwrite */
        config.autoLoad= true;
        config.autoSync= true;
        config.proxy.type= 'ajax';
        config.proxy.reader= {
            type: 'json',
            successProperty: 'success',
            root: 'data',
            messageProperty: 'message'
        };
        config.proxy.writer= {
            type: 'json',
            writeAllFields: true,
            root: 'data'
        };
        config.proxy.listeners= {

            exception: function(proxy, response, operation) {
                /* generic exception handling here */
            }
        };
        this.callParent([config]);
        this.on(
            'add',
            function(store, records, index, eOpts) {
                /* 27/04/2012 (!!) Hack: force property on all records */
                for (var i = 0; i <records.length; i++) {
                    records[i].data.id_partenaire=this.idPartenaire;
                };
            },
            this
        );
        this.on(
            'beforesync',
            /* 27/04/2012 (!!) Hack: force property on all records to false
             * and then ExtJs sees it's not the same so update
             * the value accordingly
             */
            function(sync, opts) {
                if (typeof sync.create!='undefined') {
                    for (var i = 0; i <sync.create.length; i++) {
                        sync.create[i].modified.id_partenaire=false;
                    };
                }
                if (typeof sync.update!='undefined') {
                    for (var i = 0; i <sync.update.length; i++) {
                        sync.update[i].modified.id_partenaire=false;
                    };
                }
            },
            this
        );
        /* (!!) remember the property (it's not copied
         * when calling this.callParent([config]); )
         */
        this.idPartenaire=config.idPartenaire;
    }
});

答案 1 :(得分:0)

我在ExtJs&#34; 5.1&#34;即使您已设置&#34; writeAllFields&#34;该字段也不会被发送到服务器。如果模型中未指定字段类型,则为true。 例如,使用此代码:

"fields:[
        {name:'commission_id', type:'int'},
        {name:'name'},
        {name:'surname', type:'auto'},
    ]"
即使没有更改,也会发送

commission_id。名称和姓氏如果没有更改,将无法发送。