码迷,mamicode.com
首页 > Web开发 > 详细

ExtJs 4.x Ajax简单封装

时间:2015-07-17 15:46:15      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

/**
 * Ajax 请求
 */
Ext.define("SinoCloudAjaxRequestClass", {
    constructor : function () {
        var me = this;
        var viewport = me.getViewPort();
        if(viewport){
            window.sinoCloudAjaxRequestClassLoadingMak = new Ext.LoadMask(viewport, {msg:"处理中..."});
        }
    },
    request : function (method,param, url, func,anotherFunc) {  //用于执行ajax请求,其中,func为第一个回调方法,一般用于执行成功提示,可以使用该类的commonPrompt,也可以直接处理成功请求后的业务,anotherFunc用于处理请求成功后执行的业务,可选
        var me = this;
        me.showViewportLoading();
        Ext.Ajax.request({
            url: url,
            params: param,
            method: method,
            success: function (response, options) {
                me.hideViewportLoading();
                if(func){
                    func(response, options,anotherFunc);
                }else{
                    Ext.Msg.alert(‘error‘, ‘error...did not define ajax callback function...‘);
                }

            }, failure: function (response, options) {
                me.hideViewportLoading();
                Ext.Msg.alert(‘错误‘, ‘系统错误,请稍候再试!‘);
            }
        });
    },get: function (param, url, func,anotherFunc) {
        var me = this;
        me.request("get",param,url,func,anotherFunc);
    }, post: function (param, url, func,anotherFunc) {
        var me = this;
        me.request("post",param,url,func,anotherFunc);
    },commonPrompt : function (response, options,anotherFunc) {  //一般提示
        if(anotherFunc){
            anotherFunc(response,options);
        }
        var text = response.responseText;
        if (text) {
            text = text.trim();
            var json = Ext.decode(text);
            var success = json.success;
            if (success) {
                Ext.Msg.alert(‘提示‘, "操作成功!");
            } else {
                var msg = json.msg;
                if(msg){
                    Ext.Msg.alert(‘提示‘, msg);
                }else{
                    Ext.Msg.alert(‘提示‘, "操作失败!");
                }
            }
        } else {
            Ext.Msg.alert(‘提示‘, "系统错误...");
        }
    },getViewPort : function () {
        return Ext.getBody();
    },showViewportLoading : function () {
        if(sinoCloudAjaxRequestClassLoadingMak){
            sinoCloudAjaxRequestClassLoadingMak.show();
        }
    },hideViewportLoading : function () {
        if(sinoCloudAjaxRequestClassLoadingMak){
            sinoCloudAjaxRequestClassLoadingMak.hide();
        }
    }
});

 

 

使用方式 : 

var ajax = Ext.create("SinoCloudAjaxRequestClass");
var param = {
     id : 1
};
var url = "demo.action";;
ajax.post(param,url,function(response, options){

});

或者 : 

ajax.post(param,url,ajax.commonPrompt,function(response, options){

});

ajax.commonPrompt 会自动验证返回的json的success并给出提示

 

ExtJs 4.x Ajax简单封装

标签:

原文地址:http://www.cnblogs.com/hythzx/p/4654415.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!