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%).