Search code examples
extjssencha-touchextjs-mvc

Avoid syncing certain parts of a model


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.


Solution

  • 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