Ext学习心得2

http://www.extjs.com/forum/showthread.php?74361-Solved-JsonStore-and-asp.net-web-service/page3

继上篇,由于我用的是web service所以返回的是XML,所以重写了原来的的TreeLoader

//根据Ext官方的那个重写了TreeLoader的方法
PostTreeLoader = Ext.extend(Ext.tree.TreeLoader, {
    requestData: function(node, callback) {
        if (this.fireEvent("beforeload", this, node, callback) !== false) {
            this.transId = Ext.Ajax.request({
                method: this.requestMethod,
                url: this.dataUrl || this.url,
                success: this.handleResponse,
                failure: this.handleFailure,
                scope: this,
                argument: { callback: callback, node: node },
                jsonData: this.getParams(node),
                headers: { 'Content-Type': 'application/json; charset=utf-8;' }
            });
        } else {
            // if the load is cancelled, make sure we notify
            // the node that we are done
            if (typeof callback == "function") {
                callback();
            }
        }
    },
    processResponse: function(response, node, callback) {
        var json = response.responseText;
        try {
            var o = eval("(" + json + ")").d;
            for (var i = 0, len = o.length; i < len; i++) {
                var n = this.createNode(o[i]);
                if (n) {
                    node.appendChild(n);
                }
            }
            if (typeof callback == "function") {
                callback(this, node);
            }
        } catch (e) {
            this.handleFailure(response);
        }
    }
});

其他都没变,就改写了上面的2个事件中红色的部分,害我为了这个tree搞了半天。

文章开头的那个链接是关于store的,老外果然很有想法,Store主要的问题还是关于那个返回的那个D,

    var store = new Ext.data.JsonStore({
        proxy: new Ext.data.HttpProxy({
            url: "Home.asmx/LoadPersonInfo"
                , method: 'post'
                , jsonData: {}
                , headers: { 'Content-Type': 'application/json; charset=utf-8;' }
        }),
        root: 'd.root',
        totalProperty: 'd.totalPorperty',

        idProperty: 'personId',
        fields: fields
    });
上面D对象的属性可以自己的,不一定就那个。

后台XX.asmx.cs

            var json = new { totalPorperty = MainTypeListCount, root = data };
            return json;

  上面是返回STORE的最后2句,json的返回对象是object,目的是为了去掉d:后面的那个大双引号。如果还有不明白的可以回复或者看那个链接老外的解决过程。小弟我这里也只能抛砖引玉了。

最后忘记了TreeLoader获得到的JSON语句也要是上面的那种对象形式。可怜我以前都是用Newtonsoft.Json转换成字符串再return,苦了几天了。

原文地址:https://www.cnblogs.com/nbjkj/p/1716366.html