2
Ext.define('User', {
  extend: 'Ext.data.Model',
  fields: [
    {name: 'name',  type: 'string'}
  ]
});
Ext.create('User', { 'name' : 'A', 'createdBy': 'Random' });

EXTJS 4.1 ignores 'createdBy' field while creating record. While in 7.4, it saves additional (not configured) fields too.

Output in 7.4

{
  "name": "A",
  "createdBy: "Random"
}

Can we avoid this (having 'createdBy' in data of record) through any configuration?

  • What is the problem exactly? You don't want to see this field in you model instance, or you want to skip undefined fields when sending the data to your backend? – Peter Koltai Sep 23 '21 at 17:22
  • @PeterKoltai, I don't want to see additional fields in model instance. In 4.1, getData() returns object with all configured fields, even if we create record with partial data, rest fields holds 'undefined'. In 7.4, it's not strict to configured fields, instance has only those fields which we pass on creation. – Devendra Haldankar Sep 23 '21 at 19:24
  • Yes, I think this behaviour was changed. But you can create custom JSON readers and writers to exclude undefined fields when reading or writing data. – Peter Koltai Sep 23 '21 at 19:33
  • 2
    [Here](https://docs.sencha.com/extjs/7.4.0/classic/Ext.data.Model.html#cfg-fields) it says for example: `Fields will automatically be created at read time for any for any keys in the data passed to the Model's proxy's Ext.data.reader.Reader whose name is not explicitly configured in the fields config.` – Peter Koltai Sep 23 '21 at 19:35
  • @PeterKoltai, We are currently upgrading Ext from 4.* to 7.*. Writing custom JSON reader/writer or overriding Model is least preferred option due to multiple reasons (like, changes at multiple places, override will have to be maintained in future upgrades, etc.). I don't think they have provided any explanation for this change, I searched change log too. Just hoping if someone comes with better approach, else override is what I'm going for. – Devendra Haldankar Sep 24 '21 at 14:53
  • Okay, I get it. Gook luck! – Peter Koltai Sep 24 '21 at 15:00
  • You could add `createdBy` to the field definitions with `convert` function and return undefined. Or you can use `serialize` to ensure the data are not send back to the server. Both methods are described [here](https://docs.sencha.com/extjs/7.2.0/modern/Ext.data.field.Field.html#cfg-serialize) – Dinkheller Sep 25 '21 at 11:24

1 Answers1

2

After going through Release Notes of many versions, I found this.

Migration Guide - EXTJS 4.2 to 5.0

It mentions following override, which will restore the behavior of record creation.

Ext.define('App.overrides.data.Model', {
    override: 'Ext.data.Model',
    constructor: function (data) {
        this.raw = Ext.apply({}, data);
        this.callParent(arguments);
    }
});

I don't have problem with dynamic field creation, but would have been better if they had introduced it as configurable behavior or give similar fields map to process in writer.