Read ExtJS message from ajax store

偶尔善良 提交于 2019-12-07 04:59:18

问题


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.


回答1:


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: {
  // ...



回答2:


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.




回答3:


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!




回答4:


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"});
}


来源:https://stackoverflow.com/questions/7977303/read-extjs-message-from-ajax-store

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!