一个使用QUnit开发的分页的Store

测试代码:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Ext.dx.PagingStore</title>
    <link type="text/css" rel="stylesheet" href="qunit.css"/>
    <script type="text/javascript" src="ext/ext-base.js"></script>
    <script type="text/javascript" src="ext/ext-all.js"></script>
    <script type="text/javascript" src="qunit.js"></script>
    <script type="text/javascript" src="ext/custom.js"></script>
    <script type="text/javascript">
        var store = new Ext.dx.PagingStore({
            data:[1,2,3,4,5,6,7,8],
            fields:[],
            autoLoad:false
        });
        test("beforeload", function() {
            equals(store.getCount(), 0, "count");
            equals(store.getTotalCount(), 0, "totalCount");
        });
        asyncTest("load", function() {
            expect(3);
            ok(store.load({params:{start:0,limit:5}}), "success");
            equals(store.getCount(),5,"count");
            equals(store.getTotalCount(), 8, "totalCount");
            start();
        });
        asyncTest("loadWithNoEnoughElements", function() {
            expect(2);
            ok(store.load({params:{start:0,limit:10}}), "success");
            equals(store.getCount(),8,"count");
            start();
        });
        asyncTest("loadOutOfElementBounds", function() {
            expect(2);
            ok(store.load({params:{start:10,limit:10}}), "success");
            equals(store.getCount(),0,"count");
            start();
        });
        asyncTest("autoLoad", function() {
            expect(1, "unload");
            var loaded = false;
            var store = new Ext.dx.PagingStore({
                fields:[], data:[1],autoLoad:true,
                proxy:{
                    on:function() {
                    },
                    relayEvents:function() {
                    },
                    request:function() {
                        ok(true);
                    }
                }
            });
            start();
        });
        test("shouldRaiseExceptionWhenDataWasNull", function() {
            try {
                new Ext.ds.PagingStore({});
                throw "Should raise exception when data was null";
            } catch(expected) {
                ok(true);
            }
        });

    </script>
</head>
<body>
<h1 id="qunit-header">QUnit Test Suite</h1>

<h2 id="qunit-banner"></h2>

<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup</div>
</body>
</html>

 

源码:

 Ext.dx = {};Ext.dx.PagingMemoryProxy = Ext.extend(Ext.data.MemoryProxy, {
    doRequest:function(action, rs, params, reader, callback, scope, arg) {
        params = params || {};
        var data = this.readSubRecords(params);
        var result;
        try {
            result = reader.readRecords(data);
        } catch(e) {
            this.fireEvent("loadexception", this, null, arg, e);
            this.fireEvent('exception', this, 'response', action, arg, null, e);
            callback.call(scope, null, arg, false);
            return;
        }
        result.totalRecords = this.data.length;
        callback.call(scope, result, arg, true);
    },
    readSubRecords:function(params) {
        var start = params.start || 0;
        var end = start + (params.limit || 0);
        return this.data.slice(start, end);
    }
});
Ext.dx.PagingStore = Ext.extend(Ext.data.Store, {
    constructor:function(c) {
        var data = c.data;
        if (c.data)
            delete c.data;
        Ext.dx.PagingStore.superclass.constructor.call(this, Ext.applyIf(c, {
            reader:new Ext.data.ArrayReader(c),
            proxy:new Ext.dx.PagingMemoryProxy(data)
        }));
    }
});
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值