How to fix POST request URL using REST in ExtJS 4?

3

I am using REST proxy. According to the standard, when adding a data is used the POST without any parameter in the URL. For example, if I want to add a user, my URL must be localhost/usuarios/ .

With the REST proxy of ExtJS 4, adding a new object is being used POST and URL localhost/usuarios/0 .

Here are the classes to illustrate:

Proxy

Ext.define('App.proxy.MeuProxy', {
    extend : 'Ext.data.proxy.Rest',

    require: ['Ext.MessageBox'],

    alias : 'proxy.meuproxy',
    type : 'rest',

    reader : {
        type : 'json',
        messageProperty : 'msg',
        root : 'data'
    },
    writer: {
        type: 'json',
        writeAllFields: true,
        encode: true,
        allowSingle: true,
        root: 'data'
    }
});

Store

Ext.define('App.store.Usuarios', {
    extend : 'Ext.data.Store',

    requires : ['App.model.Usuario'],

    model : 'App.model.Usuario'

});

Model

Ext.define('App.model.Usuario', {
    extend : 'Ext.data.Model',

    requires : ['App.proxy.MeuProxy'],

    idProperty : 'id',
    fields : [{
        name : 'id',
        type : 'int'
    }, {
        name : 'nome',
        type : 'string'
    }, {
        name : 'sobrenome',
        type : 'string'
    }],

    proxy : {
        type : 'meuproxy',
        url : 'usuarios/'
    }
});

I'm using getRecord() of class Ext.form.Panel to retrieve the data from my form for the template and then save() of the Ext.data.Model class to send to the server.

var novoUsuario = meuFormulario.getRecord();
novoUsuario.save(); 

The request sent to the server goes to the URL localhost/usuarios/0 . The json uploaded is:

{
    "data" : {
        "id" : 0,
        "nome" : "Maria",
        "sobrenome" : "do Socorro"
    }
}

I saw that if I did not set the type of ID, the output is as string and my URL localhost/usuarios/ (as I need it to stay), but it is not wanted because it interferes with the backend, which expects an integer value for id and nothing in the URL.

Output with auto type ID (not defined) :

{
    "data" : {
        "id" : "",
        "nome" : "Maria",
        "sobrenome" : "do Socorro"
    }
}
How can I send the URL localhost/usuarios/ (without any parameters), with POST method, and the id attribute of my model as integer ( id=0 )?

Note: In REST, editing, deleting, and selecting a user registers their id in the URL (% with%).

    
asked by anonymous 18.12.2013 / 14:15

2 answers

3

Based on the Ext.data class source code. proxy.Rest , I overwritten the buildUrl() method, as follows:

buildUrl : function(request) {
    var me = this, operation = request.operation;

            // Se o tipo de operação for 'create' não adiciona id na URL
            me.appendId = !(operation.action === "create");

    return me.callParent(arguments);
}

This change creates the limitation that the appendId parameter no longer provides in the extension or instantiation of this class the ability to include the id or not in the URL, as this mechanism is automatically managed by the new class.

    
18.12.2013 / 17:49
1

In the REST proxy by defaul the appendId parameter is true which generates a url as users / 1, inserting the id automatically. Set the parameter to false.

proxy : {
    type : 'meuproxy',
    url : 'usuarios/',
    appendId: false // default true
}
    
18.12.2013 / 16:32