是否可以在 ExtJS 中将存储字段标记为只读?
在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< 发送/code> 属性,因为它应该是只读的。有没有什么方法可以配置存储或记录以获得此行为?
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"
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您在时间戳字段上设置 {disabled: true},则不应将其提交到服务器
If you set {disabled: true} on the timestamp field, it shouldn't be submitted to server
您还可以在时间戳字段上将默认值设置为您想要的任何值。您已将 JsonWriter 配置为继续写入所有字段。如果将默认值设置为 '' 或 NULL,您也许能够获得所需的行为。
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.
您可以尝试拦截对 writer 的调用,然后删除当时的时间戳:
You could try intercepting the call to the writer and just remove the timestamp at that time:
writeAllFields
必须为true
吗?它默认为false
,如果您没有更新时间戳属性,则不会发送它。或者,您可以覆盖 <
JsonWriter
上的 code>toHash 函数。目前它有这样的评论:您可以添加自己的实现。
Does
writeAllFields
have to betrue
? It defaults tofalse
which wouldn't send the timestamp property if you didn't update it.Alternatively, you could override the
toHash
function on theJsonWriter
. At the moment it has this comment:You could add your own implementation of that.
使用
由于 Ext4,您可以在模型字段描述中
Since Ext4 you may use
in the Model field description
那么
时间戳字段将永远不会发送到服务器。
then
the timestamp field will never be send to server.