I use Model.save
to save data from the ExtJs form. Sometimes server returns operation status in following format:
{"success": false, "error": {"name": "Invalid Name"}}
The following code sends data from form to server:
var model = formPanel.getRecord(); model.save({ callback: function(record, operation, success) { // operation.response is null, // and success === true // how to read server response here? } })
Server response is treated as successful because HTTP status is 200. So I I have to read server response to check operation status. But operation.response
is null
in callback
function.
Here is my Model:
Ext.define('My.Model', { extend: 'Ext.data.Model', idProperty: 'id', fields: [ {name: 'id', type: 'auto'}, {name: 'name', type: 'auto'} ], proxy: { type: 'rest', url: 'api/v1/models', appendId: true, reader: { type: 'json', }, writer: { type: 'json' } } });
Question: how can I access server response after Model.save
's call?
More generic question: is it semantically correct to use Model.load
or Model.save
to populate/save the ExtJs form?
I'm using ExJs 5.0.1.1255.
I created some simple test code for this:
var Clazz = Ext.define('MyModel', {
extend: 'Ext.data.Model',
proxy: {
type: 'rest',
url: 'api/v1/models'
}
});
var instance = Ext.create('MyModel', {
name: 'MyName'
});
instance.save({
callback: function(record, operation) {
}
});
The server responds with:
{
success: true,
something: 'else'
}
You can see this in a fiddle here: https://fiddle.sencha.com/#fiddle/fhi
With this code, the callback has a record
argument, and record.data
contains the the original record merged with the server response. In addition, you can do operation.getResponse()
(rather than just operation.response
) to get full access to the server's response.
In regard to your question on load vs save, if you use view models and bind the model that way, it kind of becomes moot as your form should always reflect the state of the model.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments