使用ajax代理处理Sencha touch Store中的404异常

San*_*erS 8 exception-handling extjs sencha-touch

我正在尝试通过处理可能发生的各种异常来使我的代码更加健壮.一个可能是Json Web请求的404异常.看起来当Json请求获得404异常时,不会调用store.load的回调方法.

代码:

    Ext.regModel('Activiteit', {
    fields: [
        { name: 'id', type: 'int' },
        { name: 'ServerId', type: 'int', mapping: 'Id' },
        { name: 'Title', type: 'string' },
        { name: 'Description', type: 'string' },
    ],
});

Ext.regApplication({
    name: 'App',
    launch: function () {
        console.log('launch');

        var ajaxActiviteitStore = new Ext.data.Store({
            model: "Activiteit",
            storeId: 'ajaxActiviteitStore',
            proxy: {
                type: 'ajax',
                url: '/senchatest/Activiteit/Gett/',
                reader: {
                    type: 'json',
                    root: 'activiteiten'
                }
            }
        });

        ajaxActiviteitStore.load(function (records, operation, success) {
            //the operation object contains all of the details of the load operation
            console.log(success);
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

这导致在sencha-touch-debug.js的第7212行上出现"未捕获的TypeError:无法读取未定义的属性'长度"异常.我正在使用版本1.1.0的sencha touch.

堆栈跟踪:

Uncaught TypeError: Cannot read property 'length' of undefined
Ext.data.Store.Ext.extend.loadRecords  sencha-touch-debug.js:7212
Ext.data.Store.Ext.extend.onProxyLoad  sencha-touch-debug.js:7024
(anonymous function)  sencha-touch-debug.js:8742
Ext.data.Connection.Ext.extend.onComplete  sencha-touch-debug.js:17566
Ext.data.Connection.Ext.extend.onStateChange  sencha-touch-debug.js:17513
(anonymous function)  sencha-touch-debug.js:3421
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?

我通过向侦听"异常"事件的代理添加侦听器找到了一种解决方法,但我更喜欢调用存储加载的回调函数.我做错了什么,或者这是默认行为?

谢谢,

桑德

GiD*_*iDo 7

如果服务器返回错误(404,500,...),我会遇到与AjaxProxy(ST 1.1.0)相同的异常(Uncaught TypeError:无法读取未定义的属性'length').

实际上,我认为问题出在Ext.data.AjaxProxy.createRequestCallback方法上.我用这样的脏代码解决了我的问题:

var ajaxActiviteitStore = new Ext.data.Store({
    model: "Activiteit",
    storeId: 'ajaxActiviteitStore',
    proxy: {
        type: 'ajax',
        url: 'some nonexistent url',
        reader: {
            type: 'json',
            root: 'activiteiten'
        },
        listeners: {
            exception: function(store, response, op) {
               console.log('Exception !');

               // hack to avoid js exception :
               // TypeError: 'undefined' is not an object (evaluating 'records.length')
               // on line sencha-touch-debug-1-1-0.js:7212
               op.records = [];
            }
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

希望这可以提供帮助,我将在sencha-touch论坛上打开一个问题.


Xup*_* MV 2

我认为你还有另一个问题,即不存在的网址。尽管如此,尝试一下这个:

var storeException = 0; 

this.ajaxActiviteitStore = new Ext.data.Store({
    model: "Activiteit",
    storeId: 'ajaxActiviteitStore',
    proxy: {
        type: 'ajax',
        url: 'some nonexistent url',
        reader: {
            type: 'json',
            root: 'activiteiten'
        },
    listeners: {
            exception: {
            fn: function(proxy, response, operation ) {                         
                        // you can parse `response.responseText` to make some decisions
                    storeException = 404;                           
                }
        }
        }
    }
});

this.ajaxActiviteitStore.load({
    scope: this,
    callback: function (records, operation, success) {
        if (storeException==0) {
            // do something
        } else {
            alert('Service unaviable');
        }
    }
});
Run Code Online (Sandbox Code Playgroud)