Search code examples
sencha-touchsencha-touch-2

Sencha Touch 2 model.save is using POST instead of PUT in update operation


In sencha touch 2 calls to model.save() on an existing record is firing a POST instead of a PUT as expected.

I have the following model:

Ext.define('HomeInventory.model.Product', {
    extend: 'Ext.data.Model',

    config: {
        idProperty: '_id',
        fields: [
            { name: '_id', type: 'auto' },
            { name: 'name', type: 'string' },
            { name: 'barcode', type: 'string' },
            { name: 'creationDate', type: 'date' },
            { name: 'currentAmount', type: 'number' },
            { name: 'isActive', type: 'boolean'}
        ],
        validations: [
            {type: 'presence', field: 'barcode', message: 'Barcode is required'},
            {type: 'presence', field: 'name', message: 'Name is required'}
        ],
        proxy :{
            type: 'rest',
            url: 'http://localhost:3000/products',
            actionMethods: {
                create: 'POST',
                read: 'GET',
                update: 'PUT',
                destroy: 'DELETE'
            },
        }
    }
});

The json payload contains an _id field as expected for existing records, but is sent to the server using an HTTP POST instead of PUT:

{_id: "575bcd86c0eb22880c7e421e", name: "test product1", barcode: "1234", creationDate: null,…}

Save function call inside controller:

submitProduct: function(){
    Ext.Viewport.setMasked({
        xtype: 'loadmask',
        indicator: true,
        message: 'Saving product...'
    });
    debugger;
    var product = Ext.create('HomeInventory.model.Product');
    this.getProductView().updateRecord(product);
    var validation = product.validate();
    if(validation.isValid){
        var me = this;
        product.save({
            success: function(){
                Ext.Viewport.unmask();
                me.returnToMain();
        },
        failure: function(){
            Ext.Viewport.unmask();
            Ext.Msg.alert('There was an error updating the product');
            me.returnToMain();
        }
      });
    }else{
        //Show validation error
    }
}

What's wrong here?


Solution

  • Well, I found a solution. It's necessary to set phantom = false on product before calling save method.