在ExtJS中,我有一个JsonStore
配置如下:
var store = new Ext.data.JsonStore({
// basic properties
restful: true,
autoSave: true,
// json writer
writer: new Ext.data.JsonWriter({
encode: false,
writeAllFields: true
}),
// field config
fields: [
'id',
'name',
{ name: 'timestamp', type: 'date', dateFormat: 'c' }
]
});
timestamp
属性应该由服务器设置,客户端应该只读取它。但是,如果我尝试使用JsonStore
添加或更新记录:
// add a record
var record = new store.recordType({ name: 'New record' });
store.insert(0, record);
// update a record
var record = store.getAt(0);
record.set('name', 'Modified record');
发送到服务器的JSON如下所示:
{
"id": 5,
"name": "Modified record",
"timestamp": "01-06-2001T15:23:14"
}
我希望它停止发送timestamp
属性,因为它应该是只读的。是否有任何方法可以配置商店或记录以获得此行为?
答案 0 :(得分:2)
从Ext4开始,您可以使用
persist: false
模型字段描述中的
答案 1 :(得分:1)
Ext.namespace('Ext.ux');
/**
* Extension of the JsonWriter that doesn't send fields having the config option
* write set to false
* @author Friedrich Röhrs
* @verison 1.0
*
*/
Ext.ux.AdvJsonWriter = function (config) {
Ext.ux.AdvJsonWriter.superclass.constructor.call(this, config);
};
Ext.extend(Ext.ux.AdvJsonWriter, Ext.data.JsonWriter, /** @lends Ext.ux.AdvJsonWriter */{
toHash: function(rec, options) {
var map = rec.fields.map,
data = {},
raw = (this.writeAllFields === false && rec.phantom === false) ? rec.getChanges() : rec.data,
m;
Ext.iterate(raw, function(prop, value){
if((m = map[prop])){
if (m.write !== false)
data[m.mapping ? m.mapping : m.name] = value;
}
});
// we don't want to write Ext auto-generated id to hash. Careful not to remove it on Models not having auto-increment pk though.
// We can tell its not auto-increment if the user defined a DataReader field for it *and* that field's value is non-empty.
// we could also do a RegExp here for the Ext.data.Record AUTO_ID prefix.
if (rec.phantom) {
if (rec.fields.containsKey(this.meta.idProperty) && Ext.isEmpty(rec.data[this.meta.idProperty])) {
delete data[this.meta.idProperty];
}
} else {
data[this.meta.idProperty] = rec.id;
}
return data;
}
});
然后
var store = new Ext.data.JsonStore({
// basic properties
restful: true,
autoSave: true,
// json writer
writer: new Ext.ux.AdvJsonWriter({
encode: false,
writeAllFields: true
}),
// field config
fields: [
'id',
'name',
{ name: 'timestamp', type: 'date', dateFormat: 'c', write: false }
]
});
永远不会将时间戳字段发送到服务器。
答案 2 :(得分:0)
如果在时间戳字段上设置{disabled:true},则不应将其提交给服务器
答案 3 :(得分:0)
您还可以在时间戳字段中将默认值设置为您想要的任何值。您已将JsonWriter配置为继续并写入所有字段。如果将默认值设置为“'或NULL,则可能会获得所需的行为。
答案 4 :(得分:0)
您可以尝试拦截对编写者的调用,然后删除当时的时间戳:
var writer = new Ext.data.JsonWriter({
encode: false,
writeAllFields: true
});
Ext.intercept(writer, 'render', function(params, baseParams, data) {
delete data.timestamp;
});
var store = new Ext.data.JsonStore({
...
writer: writer,
...
答案 5 :(得分:0)
writeAllFields
必须是true
吗?它默认为false
,如果您没有更新它,它将不会发送timestamp属性。
或者,您可以覆盖JsonWriter
上的toHash
功能。目前它有这个评论:
TODO Implement excludes/only configuration with 2nd param
您可以添加自己的实现。