简体   繁体   English

实施Sencha Touch ListPaging插件

[英]Implement Sencha Touch ListPaging plugin

I have a simple Sencha Touch contacts/users app, which shows a list and then discloses more details. 我有一个简单的Sencha Touch联系人/用户应用程序,它显示了一个列表,然后公开了更多详细信息。

I reach my server with Ext.Ajax.request using our API to fetch the users and populate the list. 我使用我们的API通过Ext.Ajax.request到达服务器,以获取用户并填充列表。 However, the totalcount is usually above 500, so I need to implement the ListPaging plugin. 但是,totalcount通常在500以上,因此我需要实现ListPaging插件。 For security reasons, I am pretty sure I cannot do the "proxy" method (because I have to use a "token" for authenticating requests). 出于安全原因,我非常确定我不能使用“代理”方法(因为我必须使用“令牌”来认证请求)。 Maybe I am wrong; 也许我错了; documentation is sparse, so I need a boost. 文档很少,所以我需要加强。

My server returns the following: 我的服务器返回以下内容:

    data: [,…]
    hasnextpage: true
    haspreviouspage: false
    pageindex: 0
    pagesize: 9999
    success: true
    totalcount: 587
    totalpages: 14

My Store: 我的商店:

     Ext.define('MyApp.store.StudentStore',{
        extend: 'Ext.data.Store',
        config:{
           storeId: 'Students', 
           model:'MyApp.model.people',
           autoLoad:false,
           remoteFilter:true,      //just trying stuff I've read about
           sortOnFilter:true,
           clearOnPageLoad: false,
           grouper: {
                 groupFn: function(record) {
                   return record.get('lastname')[0];
                 }
           },
           sorters: 'lastname'
        }
    });

And my List View: 和我的列表视图:

Ext.define('MyApp.view.MainPanel', {
    extend: 'Ext.dataview.List',
    alias : 'widget.mainPanel',
    requires: [
        'MyApp.store.StudentStore',
        'Ext.plugin.ListPaging'
    ],

    config: {
        store : 'Students',
        model: 'people',
        grouped:true,
        sorters: 'lastname',
        itemTpl: new Ext.XTemplate(
                '<tpl for=".">'+                    
                    '<h1>{firstname:ellipsis(45)} {lastname:ellipsis(45)}</h1>' +
                    '<h4>{grade} grade</h4>' +
                '</tpl>'
        ),
        plugins: [{
            xclass: 'Ext.plugin.ListPaging',
            autoPaging: true,
            bottom: 0,
            loadMoreText: ''
        }]           
    }
});

I would like to utilize the ListPaging plugin to autoload the next 45 users when the screen is scrolled to the bottom. 当屏幕滚动到底部时,我想利用ListPaging插件自动加载接下来的45个用户。 Any advice is greatly appreciated! 任何意见是极大的赞赏!

EDIT : SOLVED!! 编辑:已解决!

@arthurakay -- you were right, my "token" was definitely getting clobbered up at some point.. Since my API requires a token for every request, I was able to create a "beforeload" function in which I set my proxy, with the token I received on login -- before the ListPaging needs to be called. @arthurakay-你是对的,我的“令牌”肯定在某个时候被破坏了。由于我的API对于每个请求都需要一个令牌,因此我能够创建一个“ beforeload”函数,在其中设置代理,我在登录时收到的令牌-需要调用ListPaging之前。 So, by the time the user is ready to scroll and activate ListPaging, my token is stored with the first record I received from the server, and magically adds 50 more records each time the user scrolls to the bottom. 因此,在用户准备滚动并激活ListPaging时,我的令牌已与我从服务器收到的第一条记录一起存储,并且每当用户滚动到底部时,都会神奇地添加50条记录。

I really hope this helps someone!! 我真的希望这对某人有帮助!

Ext.define('MyApp.store.PersonStore',{
    extend: 'Ext.data.Store',
    config:{
        storeId: 'Persons',
        model:'MyApp.model.PersonModel',
        autoLoad:false,
        clearOnPageLoad: true,

        listeners: {
            beforeload: function(store){

                store.setProxy({
                    headers: {
                        Accept : 'application/json',
                        Authorization : 'Bearer:' + this.token
                    },
                    type: 'ajax',
                    pageParam: 'pageindex',
                    url: this.url,
                    extraParams: {
                        count: this.count
                    },
                    reader: {
                        type: 'json',
                        rootProperty:'data',
                        pageParam: 'pageindex',
                        totalProperty: 'totalcount'
                    }
                });
            }
        },

        grouper: {
            groupFn: function(record) {
                return record.data.lastname[0]
            }
        },
        sorters: 'lastname'
    },

    setParams: function(params){
        for (var prop in params){
            if (params.hasOwnProperty(prop)){
                this[prop] = params[prop];
            }
        }
    }
});

And I add the 'setParams' while adding the items to the store here: 我在将商品添加到此处的商店时添加了“ setParams”:

        var feedStore = Ext.getStore('FeedStore');

        //call the setParams method that we defined in the store
        feedStore.setParams({
            token: TOKEN,
            count: 50,
            url: URL
        });

Are you sure the API docs are "sparse"? 您确定API文档“稀疏”吗?

http://docs.sencha.com/touch/2-1/#!/api/Ext.plugin.ListPaging http://docs.sencha.com/touch/2-1/#!/api/Ext.plugin.ListPaging

From the very top: 从最顶部开始:

"By specifying autoPaging: true, an 'infinite scroll' effect can be achieved, ie, the next page of content will load automatically when the user scrolls to the bottom of the list." “通过指定autoPaging:true,可以实现“无限滚动”效果,即,当用户滚动到列表底部时,将自动加载下一页内容。”

Also, what does security have to do with using the proxy? 此外,使用代理与安全性有什么关系? If you have to pass the token in each request, use the "extraParams" config on the store proxy: 如果必须在每个请求中传递令牌,请使用商店代理上的“ extraParams”配置:

http://docs.sencha.com/touch/2-1/#!/api/Ext.data.proxy.Ajax-cfg-extraParams http://docs.sencha.com/touch/2-1/#!/api/Ext.data.proxy.Ajax-cfg-extraParams

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM