是否可以在 ExtJS 中将存储字段标记为只读?

发布于 2024-10-11 03:25:40 字数 970 浏览 8 评论 0原文

在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

酒绊 2024-10-18 03:25:41

如果您在时间戳字段上设置 {disabled: true},则不应将其提交到服务器

If you set {disabled: true} on the timestamp field, it shouldn't be submitted to server

jJeQQOZ5 2024-10-18 03:25:41

您还可以在时间戳字段上将默认值设置为您想要的任何值。您已将 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.

苍景流年 2024-10-18 03:25:41

您可以尝试拦截对 writer 的调用,然后删除当时的时间戳:

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,
...

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,
...
飘逸的'云 2024-10-18 03:25:41

writeAllFields 必须为 true 吗?它默认为 false,如果您没有更新时间戳属性,则不会发送它。

或者,您可以覆盖 < JsonWriter 上的 code>toHash 函数。目前它有这样的评论:

TODO Implement excludes/only configuration with 2nd param

您可以添加自己的实现。

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.

那支青花 2024-10-18 03:25:40

使用

persist: false 

由于 Ext4,您可以在模型字段描述中

Since Ext4 you may use

persist: false 

in the Model field description

审判长 2024-10-18 03:25:40
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 }
    ]
});

时间戳字段将永远不会发送到服务器。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文