Reputation: 11
I would like to ask you a question with the use of a model that calls a data reading service which returns a json. All fields are declared in the model. When the model is used in the creation of a store, it consequently calls a data reading service which is returned in json format. When the delete service is launched, which expects to receive only the ID field, it goes into error because the store from the sync method passes all the fields. How can I make sure that only the ID field is passed to the service when calling the sync mode? I also tested it by creating a model with only one field:
fields: ['firstName', { type: 'int', name: 'ID' }], Even if only one field is specified in the model, the store is initialized with all the fields coming from the json and then it correctly executes the remove method but when the sync is launched it goes into error because all the fields are present and not just the ID.
This is the model:
Ext.define('AmpelideWeb.model.VignaToponimo', { extend: 'AmpelideWeb.model.Base',
fields: [{
name: 'ID',
type: 'int'
},
{
name: 'CODICE',
type: 'string'
},{
name: 'DESCRIZIONE',
type: 'string'
},{
name: 'SIAN_CODICE',
type: 'string'
},{
name: 'SIAN_EXPFLG',
type: 'string'
},
{
name: 'ID_REGIONE',
type: 'int'
},{
name: 'ID_PROVINCIA',
type: 'int'
}],
statics: {
baseUrl: 'vigne/toponimi'
},
});
This is the model Base:
Ext.define('AmpelideWeb.model.Base', {
extend: 'Ext.data.Model',
identifier: 'negative',
idProperty: 'ID',
inheritableStatics: {
format: 'json',
idParam: 'ID',
readMethod: 'POST',
costantUrl:'http://192.168.24.8:8080/API/v1/'
},
schema: {
proxy: {
type: 'ajax',
idParam: '{idParam}',
paramsAsJson: false,
api: {
create : '{costantUrl}'+'{baseUrl}'+'/insert',
read : '{costantUrl}'+'{baseUrl}',
update : '{costantUrl}'+'{baseUrl}'+'/edit',
destroy : '{costantUrl}'+'{baseUrl}'+'/delete'
},
actionMethods: {
create: 'POST',
read: '{readMethod}',
update: 'POST',
destroy: 'POST'
},
reader: {
type: '{format}'
},
writer: {
//rootProperty:'',
type: '{format}',
writeAllFields: true,
allowSingle: false,
},
}
}
});
This is delete method:
onDeleteClick: function () {
var form = Ext.getCmp('DettaglioVignaToponimo');
let vignaToponimoIdDelete = this.vignaToponimoId;
let store = Ext.create('Ext.data.Store', {
model: 'AmpelideWeb.model.VignaToponimoDelete',
});
store.load();
store.proxy.paramsAsJson = true;
if (form.getValues().ID!==null){
Ext.Msg.confirm('Eliminazione Vigne Toponimi', 'Se sicuro di voler eliminare il dato selezionato ?', function (id, value) {
if (id === 'yes') {
store.proxy.headers = {'Content-Type': 'application/x-www-form-urlencoded'};
const record = new AmpelideWeb.model.VignaToponimoDelete({ID: vignaToponimoIdDelete});
store.remove(record);
store.sync({
success: function (batch) {
Ext.Msg.alert('Eliminazione dati Vigne Toponimi!', 'Eliminazione Vigne Toponimi avvenuta correttamente', function (btn) {
});
},
failure: function (batch, opt) {
responseObj = batch.exceptions[0].error.response.responseJson;
Ext.Msg.alert('Eliminazione Vigne Toponimi fallita', 'messaggio di errore: ' + responseObj.message);
}
});
}else{
return false;
}
})
}else{
Ext.Msg.alert('Eliminazione Vigne Toponimi fallito', 'messaggio di errore: ID Vigne Toponimi è null, nessun dato da eliminare.');
}
},
Upvotes: 0
Views: 191
Reputation: 432
Even if only one field is specified in the model, the store is initialized with all the fields coming from the json and then it correctly executes the remove method but when the sync is launched it goes into error because all the fields are present and not just the ID.
Two things: Ext dynamically adds fields into models based on data. It encourages to not maintain field metadata and make it more flexible. writeAllFields is true, that's why proxy sending all parameters.
writeAllFields make sense only for UPDATE operation, but anyways we don't have a control on request type here.
As I understand, you want to process the data before sending request. I think transform of Ext.data.writer.Writer will be good approach.
You can have in writer it like below:
writer: {
type: 'json',
transform: function(data, request) {
var requestAction = request.getAction(),
serialized;
// keep only id if a delete operation
serialized = (requestAction == "delete") ? { id: data.id } : data;
return serialized;
}
}
Upvotes: 2