ExtJS、存储、HasMany - BelongsTo 和更新过程:如何?

2023-11-21

我有两个数据模型:Writer.AttributValeur and Writer.Produit.

Writer.Produit has HasMany / BelongsTo有关系Writer.AttributValeur.

因此定义是这样的:

Ext.define('Writer.AttributValeur', {
    extend: 'Ext.data.Model',
    fields: [{
            name: 'id',
            type: 'int',
            useNull: true
        },  
        'description',
        'val'
    ],  
    belongsTo: 'Writer.Produit'
});

Ext.define('Writer.Produit', {
    extend: 'Ext.data.Model',
    fields: [{
            name: 'id',
            type: 'int',
            useNull: true
        },  
        'titre',
        'description'
    ],
    hasMany: {
        model: 'Writer.AttributValeur',
        name: 'attributs'
    }   
});

var store = Ext.create('Ext.data.Store', {
    model: 'Writer.Produit',
    autoLoad: true,
    autoSync: true,
    proxy: {
        type: 'ajax',
        api: {
            read: 'json/liste_view/',
            create:  'json/item/?mode=create',
            update:  'json/item/?mode=update',
            destroy: 'json/item/?mode=destroy'
        },
        reader: {
            type: 'json',
            successProperty: 'success',
            root: 'data',
            messageProperty: 'message'
        },
        writer: {
            type: 'json',
            writeAllFields: true,
            root: 'data'
        }
    }
});

现在,当我阅读该文件并询问“Produits”时,有一个完美运行的 AJAX 答案:

AJAX answer that works perfectly

而每一“行”中,都有很多Writer.AttributValeur(我将它们别名为“属性”,见图):

many Writer.AttributValeur

问题是当我插入一个Writer.AttributValeur在这个“attributs“字段,像这样:

form.getRecord().attributs().add(newrecord);

它工作完美,但是当我打电话时store.sync()什么都没发生。所以我用手将记录标记为dirty:

form.getRecord().attributs().add(newrecord);
form.getRecord().setDirty();
form.getRecord().store.sync();

现在已经发送了,但是attributs没有发送!看:

Showing that the attributs are not sent

我该如何将其“添加”到更新过程中?


这是覆盖的东西:

Ext.data.writer.Json.override({
  {*/*
     * This function overrides the default implementation of
     * json writer. Any hasMany relationships will be submitted
     * as nested objects
     */*}
    getRecordData: function(record) {
        var me = this, i, association, childStore, data = {}; 
        data = me.callParent([record]);

        /* Iterate over all the hasMany associations */
        for (i = 0; i < record.associations.length; i++) {
            association = record.associations.get(i);
            if (association.type == 'hasMany')  {
                data[association.name] = []; 
                childStore = eval('record.'+association.name+'()');

                //Iterate over all the children in the current association
                childStore.each(function(childRecord) {

                    //Recursively get the record data for children (depth first)
                    var childData = this.getRecordData.call(this, childRecord);
                    if (childRecord.dirty | childRecord.phantom | (childData != null)){
                        data[association.name].push(childData);
                        record.setDirty();
                    }   
                }, me);
            }   
        }   
        return data;
    }   
}); 

这是我如何使用它的示例:

var store = Ext.create('Ext.data.Store', {
    model: 'Writer.Produit',
    autoLoad: true,
    autoSync: true,
    proxy: {
        type: 'ajax',
        api: {
            read: 'json/liste_view/',
            create:  'json/item/?mode=create',
            update:  'json/item/?mode=update',
            destroy: 'json/item/?mode=destroy'
        },
        reader: {
            type: 'json',
            successProperty: 'success',
            root: 'data',
            messageProperty: 'message'
        },
        writer: new Ext.data.writer.Json( {
            type: 'json',
            writeAllFields: true,
            root: 'data'
        })
    }
});

当我添加嵌套记录时,我是这样做的attributs这是 oneToMany 关联(参见我的问题)。需要注意的是我设置了 Dirty全部嵌套记录,以便我确定它们已发送:

var rec = this.formDst.getRecord(),
    atts = rec.attributs();
atts.add(sel);
for (var i = 0; i <atts.data.items.length; i++) {
    atts.data.items[i].setDirty();
};
rec.setDirty();
rec.store.sync();
this.close();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ExtJS、存储、HasMany - BelongsTo 和更新过程:如何? 的相关文章

随机推荐