论坛首页 Web前端技术论坛

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

浏览 1652 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-06-22  

测试代码:

<!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)
        }));
    }
});
 
论坛首页 Web前端技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics