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

Asked

Viewed 633 times

3

I am using REST proxy. According to the default, 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 should be localhost/usuarios/.

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

Follow 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 am using the getRecord() class Ext.form.Panel to redeem my form data for the template and then, save() class Ext.data.Model 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 sent is:

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

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

Auto type ID output (not defined):

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

How can I submit the URL localhost/usuarios/ (without any parameter), with POST method, and the id attribute of my model as integer (id=0)?

Note: Remembering that, in REST, the editing, deletion and selection of a user registers his id in the URL (localhost/usuarios/2).

2 answers

3


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

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 that class the ability to include the id or not in the URL, as this mechanism is automatically managed by the new class.

1

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

proxy : {
    type : 'meuproxy',
    url : 'usuarios/',
    appendId: false // default true
}
  • Thank you so much for the help! I actually needed something that only worked for the type of POST method (or 'CREATE' operation). Based on your reply I made mine which I posted here. Anyway, I gave an upvote for your help.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.