ExtJS,store,HasMany - BelongsTo和更新过程:howto?

8 extjs extjs4

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

Writer.ProduitHasMany/ 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'
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

现在,当我阅读文件,询问"Produits"时,有一个完美的AJAX答案:

AJAX的答案非常有效

在每个"行"中,有很多Writer.AttributValeur(我把它们别名为"attributs"见图):

很多Writer.AttributValeur

问题是我Writer.AttributValeur在这个" attributs"字段中插入一个,如下所示:

form.getRecord().attributs().add(newrecord);
Run Code Online (Sandbox Code Playgroud)

它完美无缺,但是当我打电话时store.sync()没有任何反应 所以我手工将记录标记为dirty:

form.getRecord().attributs().add(newrecord);
form.getRecord().setDirty();
form.getRecord().store.sync();
Run Code Online (Sandbox Code Playgroud)

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

显示没有发送attributs

如何将此"添加"到更新过程中?

小智 8

这是覆盖的东西:

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;
    }   
}); 
Run Code Online (Sandbox Code Playgroud)

这是我如何使用它的一个例子:

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'
        })
    }
});
Run Code Online (Sandbox Code Playgroud)

当我在这里添加一个嵌套记录时,我是如何做到的,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();
Run Code Online (Sandbox Code Playgroud)

  • 我可以建议你改变`eval('record.'+ association.name +'()');`到`record [association.name]();` (4认同)