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
).
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.
– José Filipe Lyra