职业IT人-IT人生活圈

 找回密码
 成为会员
搜索
查看: 383|回复: 1

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

[复制链接]
钰云 发表于 2011-8-24 11:38 | 显示全部楼层 |阅读模式
测试代码:
Html代码  
<!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>  

<!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>
源码:
Js代码  
<SPAN style="WHITE-SPACE: normal"> <SPAN style="WHITE-SPACE: pre">Ext.dx = {};</SPAN></SPAN>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)   
        }));   
    }   
});  

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)
        }));
    }
});  
qunit.rar (12.6 KB)
下载次数: 7

ksdal 发表于 2011-8-24 11:39 | 显示全部楼层
推荐链接
见证又一个准百万富翁的诞生!

20-30万急聘多名天才Java/MTA软件工程师
3G培训就业月薪平均7K+,不3K就业不花一分钱!

您需要登录后才可以回帖 登录 | 成为会员

本版积分规则

QQ|手机版|小黑屋|网站帮助|职业IT人-IT人生活圈 ( 粤ICP备12053935号-1 )|网站地图
本站文章版权归原发布者及原出处所有。内容为作者个人观点,并不代表本站赞同其观点和对其真实性负责,本站只提供参考并不构成任何投资及应用建议。本站是信息平台,网站上部分文章为转载,并不用于任何商业目的,我们已经尽可能的对作者和来源进行了通告,但是能力有限或疏忽造成漏登,请及时联系我们,我们将根据著作权人的要求立即更正或者删除有关内容。

GMT+8, 2024-5-10 03:20 , Processed in 0.145322 second(s), 20 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表