![](/img/trans.png)
[英]Extjs 6.5 - finding an association type (hasOne, hasMany, belongsTo, etc)
[英]ExtJS, store, HasMany - BelongsTo and update process: howto?
我有兩個數據模型: Writer.AttributValeur
和Writer.Produit
。
Writer.Produit
擁有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答案:
在每個“行”中,有許多Writer.AttributValeur
(我把它們別名為“attributs”見圖):
問題是當我在這個“ attributs
”字段中插入Writer.AttributValeur
時,如下所示:
form.getRecord().attributs().add(newrecord);
它工作得很好但是當我調用store.sync()
沒有任何反應。 所以我手工將記錄標記為dirty
:
form.getRecord().attributs().add(newrecord);
form.getRecord().setDirty();
form.getRecord().store.sync();
現在已經發送了,但是發送的是attributs
! 看到:
如何將此“添加”到更新過程中?
這是覆蓋的東西:
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關聯的attributs
(請參閱我的問題)。 重要的是要注意我設置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();
我喜歡簡單的解決方案所以我還添加了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;
}
});
從sencha文檔 - 看起來你需要在form.getRecord().attributs()
上調用sync而不是在主記錄上。 你能試試嗎?
http://docs.sencha.com/ext-js/4-0/#!/api/Ext.data.HasManyAssociation
4.0x中不支持此功能,據我所知,尚未在4.1的功能路徑上使用此功能。 然而,已經有一些嘗試解決了這個問題,請看這個主題: http : //www.sencha.com/forum/showthread.php? 141957-Saving-objects-that-are-linked-hasMany-relation-with-a- single -商店
表格的另一個有趣的方法:Jamie Sutherland的https://stackoverflow.com/a/9891694/834424
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.