2011-03-30 59 views
1

我有以下的ExtJS。当响应成功时(响应是JSON,如:{“success”:true,“message”:“......”}),调用监听器“write”。但是,如果回复不成功,我该如何附加回调? ({ “成功”:假的, “消息”: “......”})Ext.data.HttpProxy回调失败

tableStructure.proxy = new Ext.data.HttpProxy({ 
     api: { 
      read: '/controller/tables/' + screenName + '/getstructure/' + table, 
      create: '/controller/tables/' + screenName + '/createcolumn/' + table, 
      update: '/controller/tables/' + screenName + '/updatecolumn/' + table, 
      destroy: '/controller/tables/' + screenName + '/destroycolumn/' + table 
     }, 

     listeners: { 
      write: tableStructure.onWrite 
     } 
    }); 

回答

4

你想赶上HTTPPROXY的exception事件。

listeners: { 
     write: tableStructure.onWrite 
     exception: function(proxy, type, action, options, response, arg) { 
      if(type === 'remote') { // success is false 
       // do your error handling here 
       console.log(response); // the response object sent from the server 
      } 
     } 
    } 

你可以找到在内线文档完整的文档为Ext.data.HttpProxy在事件部分下降。

0

您应该能够利用write事件本身。写事件的签名是: write(dataproxy,action,data,response,record,options)

您可以从操作对象访问成功变量并检查值是true还是false。您应该能够访问成功变量:

action.result.success 

你可以这样做:

if(action.result.success != true) { 
    // If success is not true 
} else { 
    // If success is true 
} 
+0

我已经试过了(似乎是显而易见的)。但是,当成功时,回调不会被解雇 – 2011-03-30 19:26:25

0

您也可以在Ext.data.Store包裹HttpProxy设置exception handler,只要您发送以外的响应码比200

var store = new CQ.Ext.data.Store({ 
     proxy : new CQ.Ext.data.HttpProxy({ 
      method : "GET", 
      url : '/some_url' 
     }), 
     reader : new CQ.Ext.data.JsonReader(), 
     baseParams : { 
      param : 'some value' 
     } 
    }); 

    store.on("beforeload", function() { 
     CQ.Ext.getBody().mask("Please wait...", false); 
    }); 

    store.on("exception", function() { 
     CQ.Ext.getBody().unmask(); 
     CQ.Ext.Msg.show({ 
      title: 'Error', 
      msg: '<span style="color:red">Bad request.</span><br/>', 
      icon: CQ.Ext.Msg.ERROR, 
      buttons: CQ.Ext.Msg.OK 
     }); 
    });