Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EXTJS model idProperty field populated with model name?

Tags:

rest

extjs5

I am trying to create a simple ExtJS5 app against a Restful RubyonRails backend. For some reason when I instantiate a model ExtJs populates the "idProperty" field with the name of the model (and counter). e.g.

{"Traffic_id":"MyApp.model.Person-1","external_id":0,...

I thought the "idProperty" field is essentially the primary key of the data record and is normally set when the record is inserted in to the DB (autoincrement)

So this field should be null or similar as the model has yet to be added to the store and syncd to the backend.

Whats even more amaising is that the field is defined as 'int' and ExtJS puts a String in it!

Can someone tell me what is going on?

Peter

Below is an app.js to Fiddle with:

Ext.application({
    name : 'Fiddle',

    launch : function() {

        var model = Ext.create('MyApp.model.Person');

       Ext.Msg.alert('Fiddle', JSON.stringify(model.data));

    }
});

Ext.define('MyApp.model.Person', {
    extend: 'Ext.data.Model',
    idProperty: 'Traffic_id',

    proxy: {
        type: 'rest',
        // url: '/traffics.json',
        format: 'json',
        api: {
            create: 'traffics',
            read: 'traffics',
            update: 'traffics/edit',
            destroy: 'traffics'
        },

        reader: {
            type: 'json',
            rootProperty: 'traffic',
            successProperty: 'success',
            messageProperty: 'message'
        },
        writer: {
            type: 'json',
            //writeAllFields  : false,
            //encode: true,
            rootProperty: 'traffic'
        },
        afterRequest: function(req, res) {
            console.log("Ahoy!", req.operation.response);
        },
        listeners: {
            exception: function(proxy, response, operation) {
                //debugger;
                Ext.MessageBox.show({
                    title: 'XXX REMOTE EXCEPTION',
                    msg: operation.getError(),
                    icon: Ext.MessageBox.ERROR,
                    buttons: Ext.Msg.OK
                });
            }
        }
    },

    fields: [{
        name: 'Traffic_id',
        type: 'int'
    }, {
        name: 'external_id',
        type: 'int'
    }, {
        name: 'description',
        type: 'string'
    }, {
        name: 'insertUser',
        type: 'string'
    }, {
        name: 'insertDate',
        type: 'date'
    }]

});
like image 208
Rhubarb65 Avatar asked Oct 07 '14 09:10

Rhubarb65


1 Answers

Set config options: persist: false. When creating it will not send the value to the server. This option is useful when fields are used to keep state on the client but do not need to be persisted to the server.Ext.data.field.Field.persist

Ext.define('MyApp.model.Person', {
    extend: 'Ext.data.Model',
    idProperty: 'Traffic_id',
    fields: [{
        name: 'Traffic_id',
        type: 'int',
        persist: false
    }, {
        name: 'external_id',
        type: 'int'
    }, {
        name: 'description',
        type: 'string'
    }, {
        name: 'insertUser',
        type: 'string'
    }, {
        name: 'insertDate',
        type: 'date'
    }]
});
like image 177
ajokn Avatar answered Nov 04 '22 20:11

ajokn