ExtJS、商店、HasMany - BelongsTo 和更新过程:如何? [英] ExtJS, store, HasMany - BelongsTo and update process: howto?

查看:21
本文介绍了ExtJS、商店、HasMany - BelongsTo 和更新过程:如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two data models: Writer.AttributValeur and Writer.Produit.

Writer.ProduitWriter.AttributValeurHasMany/BelongsTo 关系.

Writer.Produit has HasMany / BelongsTo relationship with 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'
        }
    }
});

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

Now, when I read the file, asking for "Produits", there's an AJAX answer that works perfectly:

并且在每个行"中,有许多 Writer.AttributValeur(我将它们别名为属性",见图):

And in each "row", there are many Writer.AttributValeur (I've aliased them as "attributs" see picture):

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

The problem is when I insert a Writer.AttributValeur in this "attributs" field, like this:

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

它工作得很好,但是当我调用 store.sync() 时什么也没有发生.所以我手动将记录标记为 dirty:

It works perfectly but when I call store.sync() nothing happens. So I mark by hand the record as dirty:

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

现在发送了,但是attributs没有发送!见:

Now it's sent, but the attributs are not sent! See:

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

How shall I do to "add" this into the update process?

推荐答案

这里是覆盖的东西:

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

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

And here's an example of how I'm using it:

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 关联(请参阅我的问题).重要的是要注意,我设置了脏所有嵌套记录,以便我确定它们已发送:

And when I add a nested record here's how I do this with attributs which is oneToMany association (see my question). It's important to note that I set Dirty all nested records so that I'm sure they're sent:

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

这篇关于ExtJS、商店、HasMany - BelongsTo 和更新过程:如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆