ExtJs5: How to read server response in Model.save

Andrey

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.

Colin Ramsay

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to save a response to a model

From Dev

read a response from socket server and save into database

From Dev

ExtJS 4.1 - Returning Associated Data in Model.Save() Response

From Dev

ExtJS 4.1 - Returning Associated Data in Model.Save() Response

From Dev

How to redraw or repaint surface in ExtJS5?

From Dev

How to read multiline response from FTP server?

From Dev

ExtJS5 MVVC the CRUD ajax proxy does read but not the other

From Dev

How to read value when using model.save() or fetch()?

From Dev

How to send data to another server and save the response via socket

From Dev

Extjs5: How to query a component by various properties

From Dev

How to properly declare links in extjs5 viewmodel?

From Dev

How to change the label of bar chart in Extjs5?

From Dev

How can addCls to Button with ViewModel binding in ExtJS5

From Dev

How to send an extra data to the server using Backbone model save?

From Dev

How to read json response from node js server?

From Dev

How to read ICollection from Model in MVC 5

From Dev

error 406 Inacceptable Spring MVC 4 Extjs5 JSON response

From Dev

How to read JSON response?

From Dev

how to save html5 canvas to server

From Dev

Put set of values from a json string into the form using view model in extjs5

From Dev

ExtJS5 Store filter "Uncaught TypeError: Cannot read property 'get' of undefined"

From Dev

Rails 4 - Titleize on read, downcase on save, in model

From Dev

How to save the response From My server , and How can i access That data

From Dev

How to save the “chunks” of a http response?

From Dev

how to save AJAX RESPONSE in Laravel

From Dev

ExtJS 5 how to dynamically create fields in model

From Dev

Backbone try to update model in server after save

From Dev

Save model object to SQL Server from R

From Java

Tensorflow: how to save/restore a model?

Related Related

  1. 1

    How to save a response to a model

  2. 2

    read a response from socket server and save into database

  3. 3

    ExtJS 4.1 - Returning Associated Data in Model.Save() Response

  4. 4

    ExtJS 4.1 - Returning Associated Data in Model.Save() Response

  5. 5

    How to redraw or repaint surface in ExtJS5?

  6. 6

    How to read multiline response from FTP server?

  7. 7

    ExtJS5 MVVC the CRUD ajax proxy does read but not the other

  8. 8

    How to read value when using model.save() or fetch()?

  9. 9

    How to send data to another server and save the response via socket

  10. 10

    Extjs5: How to query a component by various properties

  11. 11

    How to properly declare links in extjs5 viewmodel?

  12. 12

    How to change the label of bar chart in Extjs5?

  13. 13

    How can addCls to Button with ViewModel binding in ExtJS5

  14. 14

    How to send an extra data to the server using Backbone model save?

  15. 15

    How to read json response from node js server?

  16. 16

    How to read ICollection from Model in MVC 5

  17. 17

    error 406 Inacceptable Spring MVC 4 Extjs5 JSON response

  18. 18

    How to read JSON response?

  19. 19

    how to save html5 canvas to server

  20. 20

    Put set of values from a json string into the form using view model in extjs5

  21. 21

    ExtJS5 Store filter "Uncaught TypeError: Cannot read property 'get' of undefined"

  22. 22

    Rails 4 - Titleize on read, downcase on save, in model

  23. 23

    How to save the response From My server , and How can i access That data

  24. 24

    How to save the “chunks” of a http response?

  25. 25

    how to save AJAX RESPONSE in Laravel

  26. 26

    ExtJS 5 how to dynamically create fields in model

  27. 27

    Backbone try to update model in server after save

  28. 28

    Save model object to SQL Server from R

  29. 29

    Tensorflow: how to save/restore a model?

HotTag

Archive