Reputation: 3755
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.
Upvotes: 2
Views: 2315
Reputation: 2206
Using model.save()
and Model.load()
is definitely the correct thing to do.
In addition to providing a custom callback, you should investigate configuring a custom proxy. In a custom proxy, you can provide your own implementation of the extractResponseData
method. This would let you centralise your need to examine the server response.
Upvotes: 1
Reputation: 16466
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.
Upvotes: 2