Marc
Marc

Reputation: 6771

Read ExtJS message from ajax store

I have an ExtJS store with an ajax proxy and json reader:

Ext.create('Ext.data.Store', {
    proxy: {
        type: 'ajax',
        url: '...',
        reader: {
            type: 'json',
            root: 'data',
            totalProperty: 'totalCount',
            messageProperty: 'message',
            successProperty: 'success'
        },
    ...

This is what I get from the server:

data: [...]
message: "I want to read this string after the store is loaded"
success: true
totalCount: x

Now I want to access the 'message' when the store is loaded - where do I get it? I looked a lot but I can't find a place to hook in? The only listener in the proxy is exception, that doesn't really help me.

Upvotes: 6

Views: 7846

Answers (4)

Motorro
Motorro

Reputation: 387

I get the message in the following way although I load manually and do not use events here:

var initData = Ext.create('My.data.SomeAjaxStore');

initData.load(function(records, operation, success) {
    if (!success || operation.hasException()) {
    // Here is your message from server
    // In case of HTTP error you get:
    //  {
    //    status: 404,
    //    statusText: "Not Found"
    //  }
    var error = operation.getError();
    Ext.log({msg:[Ext.getClassName(me), ": Command failure: ", error].join(""), level:"error"});
}

Upvotes: 0

mfruizs
mfruizs

Reputation: 770

In other case:

myStore.load({
   callback : function(object, response, success) {
    // on error response: success = false
    if(!success) {
        // i don't remember de correct path to get "message" or "responseText"
        console.log(response.response.responseText);
    } else {
         ... 
    }
});

Cya!

Upvotes: 0

boggy
boggy

Reputation: 4027

My answer applies to ExtJs 4.1.x. I spent some time reading the code and it seems that one way to do this is to provide a callback in the store beforeload event instead of handling the load event. The callback is passed the operation object which will contain the original request parameters and in case of success it will contain the response object and the data (parsed) under the resultSet property.

Upvotes: 0

Molecular Man
Molecular Man

Reputation: 22386

use store load event:

Ext.create('Ext.data.Store', {
  listeners: {
    'load': function(store, records, successful, operation) {
      alert(operation.resultSet.message);
    }
  },
  proxy: {
  // ...

UPDATE

It appears that documentation for load event is wrong. The correct list of arguments is (store, records, successful) (no operation argument). Therefore the solution above wouldn't work.

However there is reader's rawData property which can help:

Ext.create('Ext.data.Store', {
  listeners: {
    'load': function(store, records, successful) {
      alert(store.getProxy().getReader().rawData.message);
    }
  },
  proxy: {
  // ...

Upvotes: 5

Related Questions