2

I have a problem. I created a simple model and tried to save new value by using it through ajax request. But parameters which must be empty sends default value. You can see it by link under. The code does not specifically set the correct way bacause of what the console(f12) can be seen fallen challenge. In it I pass a value through a query-string, as well as through the payload-request (not yet invented how to get rid of it, as I understand it-payload is used by default). In general, instead of an empty carId call transfers Car-1.

https://fiddle.sencha.com/#fiddle/dsj

How do I fix this behavior and do that if we do not share any meaning, it passed empty?

Dmitriy
  • 33
  • 7
  • [Is there any way to disable “idProperty” of Model in Extjs?][1] [1]: http://stackoverflow.com/questions/29562263/is-there-any-way-to-disable-idproperty-of-model-in-extjs – ajokn Apr 15 '15 at 09:18

3 Answers3

1

You can create your custom proxy class that extends Ext.data.proxy.Ajax and then override buildRequest method to check for all create actions and to assign desired value to idProperty

Ext.define('CarProxy', {
    extend: 'Ext.data.proxy.Ajax',

    alias: 'proxy.carproxy',
    type: 'ajax',
    idParam: 'carId',
    reader: {
        type: 'json',
        rootProperty: 'data'
    },
    api: {
        create: './createcar.json'
    },
    writer: {
        type: 'form'
    },

    buildRequest: function(operation) {
        var request = this.callParent(arguments);
        if (request.getAction() === 'create') {
            request.getRecords().forEach(function(record) {
                record.set('carId', ''); //assing desired value to id
            });
        }

        return request;
    }
});
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Davor Zubak
  • 4,726
  • 13
  • 59
  • 94
0

This is default ExtJS behaviour. If you do not specify id, it is generated. To avoid that you can for add constructor to your model:

Ext.define("Car", {
    [...],

    constructor: function() {
        this.callParent(arguments);
        // check if it is new record
        if (this.phantom) {
            // delete generated id
            delete this.data[this.idProperty];
        }
    }
});
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Krzysztof
  • 15,900
  • 2
  • 46
  • 76
0

Thanks everyone who answered. I want to show my solution:

add to model definition parameter identifier: 'custom' . And then create appropriate identifier which will return undefined on generate method:

Ext.define('Custom', {
    extend: 'Ext.data.identifier.Generator',
    alias: 'data.identifier.custom',

    generate: function() {
        return;
    }
});
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
Dmitriy
  • 33
  • 7