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

时间:2012-03-26 11:41:29

标签: extjs extjs4

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

Writer.ProduitHasManyBelongsTo / 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(我把它们别名为“attributs”见图片):

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

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

4 个答案:

答案 0 :(得分: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;
    }   
}); 

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

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();

答案 1 :(得分:1)

我喜欢简单的解决方案,所以我还添加了belongsTo支持:

Ext.data.writer.Json.override({
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);
        }

        if(association.type == 'belongsTo') {

            // we need ucfirst
            var method = 'get' + association.name.charAt(0).toUpperCase() + association.name.slice(1);
            var childRecord = eval('record.' + method + '()');
            var childData = this.getRecordData.call(this, childRecord);

            if (childRecord.dirty | childRecord.phantom | (childData != null)) {
                data[association.name] = childData;
                record.setDirty();
            }

        }

    }
    return data;
}

});

答案 2 :(得分:0)

来自sencha文档 - 看起来你需要在form.getRecord().attributs()上调用同步而不是主记录上的同步。你能试试吗?

http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.HasManyAssociation

答案 3 :(得分:0)

据我所知,4.0x中不支持此功能,但尚未在4.1的功能路径上使用此功能。 然而,有一些尝试解决,请参阅此主题:http://www.sencha.com/forum/showthread.php?141957-Saving-objects-that-are-linked-hasMany-relation-with-a-single-Store

表格另一种有趣的方法:Jamie Sutherland的https://stackoverflow.com/a/9891694/834424