避免同步模型的某些部分

发布于 2024-12-08 11:11:59 字数 683 浏览 0 评论 0原文

假设我有一个像这样的 Sencha Touch 模型:

Ext.regModel('User', {
    'fields': [{
        'name': 'first_name',
        'type': 'string'
     }, {
        'name': 'last_name',
        'type': 'string'
     }, {
        'name': 'full_name',
        'type': 'string',
        'convert': function(v, record) {
            return record.data.first_name + ' ' + record.data.last_name;
        }
     }, {
        'name': 'age',
        'type': 'integer'
     }
]});

服务器在获取记录时返回“first_name”、“last_name”和“age”字段。但是,当我更新用户的名字和/或姓氏,并在商店上调用 sync() 时,它会将所有字段发送到服务器,包括全名和年龄,即使我不这样做我不想那样。

我知道我可以忽略服务器上的数据,但在某些情况下(例如,有很多带有“转换”的字段),它会给有效负载增加很多不必要的开销。

Let's say I have a Sencha Touch model like this:

Ext.regModel('User', {
    'fields': [{
        'name': 'first_name',
        'type': 'string'
     }, {
        'name': 'last_name',
        'type': 'string'
     }, {
        'name': 'full_name',
        'type': 'string',
        'convert': function(v, record) {
            return record.data.first_name + ' ' + record.data.last_name;
        }
     }, {
        'name': 'age',
        'type': 'integer'
     }
]});

The server returns the 'first_name', 'last_name', and 'age' fields when getting records. However, when I update a user's first and/or last name, and I call sync() on the store, it will send all fields to the server, including full_name and age, even though I don't want that.

I know I could just ignore that data on the server, but in some cases (with lots of fields with 'convert' for example) it adds lots of unnecessary overhead to the payload.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

抽个烟儿 2024-12-15 11:11:59

您是否尝试过该字段的配置 persist: false

编辑 我想您可以为代理创建自己的编写器并覆盖 getRecordData 方法。

Ext.define('Ext.ux.NewWriter', {
    extend: 'Ext.data.Writer',
    getRecordData: function(record) {
        return {
            first_name: record.data.first_name,
            last_name: record.data.last_name,
            age: record.data.age
        };
    }
});

...
proxy: {
    writer: Ext.create('Ext.ux.NewWriter');
}

希望有帮助

Have you tried the config persist: false for the field?

Edit I guess you could create your own writer for the proxy and override the getRecordData method.

Ext.define('Ext.ux.NewWriter', {
    extend: 'Ext.data.Writer',
    getRecordData: function(record) {
        return {
            first_name: record.data.first_name,
            last_name: record.data.last_name,
            age: record.data.age
        };
    }
});

...
proxy: {
    writer: Ext.create('Ext.ux.NewWriter');
}

Hope it helps

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