Is it possible to mark a store field as readonly in ExtJS?
In ExtJS, I have a JsonStore
configured like this:
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' }
]
});
The timestamp
property should be set by the server, and the client should only read it. However, if I try to add or update a record using the 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');
The JSON that's being sent over to the server looks like this:
{
"id": 5,
"name": "Modified record",
"timestamp": "01-06-2001T15:23:14"
}
开发者_如何学Python
I'd like it to stop sending over the timestamp
property, since it's supposed to be read-only. Is there any way of configuring either the store or the record in order to get this behavior?
Since Ext4 you may use
persist: false
in the Model field description
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;
}
});
then
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 }
]
});
the timestamp field will never be send to server.
If you set {disabled: true} on the timestamp field, it shouldn't be submitted to server
You can also set the default value to whatever you want on the timestamp field. You have configured your JsonWriter to go ahead and write to all fields. If you set the default value to '' or NULL you might be able to get the desired behavior you want.
You could try intercepting the call to the writer and just remove the timestamp at that time:
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,
...
Does writeAllFields
have to be true
? It defaults to false
which wouldn't send the timestamp property if you didn't update it.
Alternatively, you could override the toHash
function on the JsonWriter
. At the moment it has this comment:
TODO Implement excludes/only configuration with 2nd param
You could add your own implementation of that.
精彩评论