如何在拉取刷新时添加自定义更新?

时间:2012-05-18 19:06:03

标签: sencha-touch-2 pull-to-refresh

在标准"拉到刷新"插件,列表存储得到更新。但是,我有两个列表,我需要为我的详细列表更新不同的商店。如何覆盖更新事件并重新加载我的其他商店?我尝试添加一个简单的监听器,但它没有触发。

[更新]

我从Sencha site获得了这个代码片段来工作:

plugins: [
          {
             xclass: 'Ext.plugin.PullRefresh',
              pullRefreshText: 'Pull down for more new Events!',
              refreshFn: function(plugin) {
                  console.log( "I'm pulled" );
              }
           }
          ]

原始代码:

Ext.define('SenchaFiddle.view.ListView', {
    extend: 'Ext.dataview.List',
    xtype: 'main-list',

    config: {
        plugins: [
            'pullrefresh',
            {
                pullRefreshText: 'Do it!',
                type: 'listpaging',
                // Don't offer "Load More" msg
                autoPaging: false,

                refreshFn: function() {             
                  console.log("Boom");
                },

                listeners: {
                    'updatedata': function(plugin, list) {
                        console.log("Getting the data");
                    }
                }

            }
        ],
        layout: 'fit',
        width: 300,
        itemTpl: '{text}'

    }
});

3 个答案:

答案 0 :(得分:6)

在Sencha Touch 2.2中,他们从Ext.util.PullRefresh中删除了refreshFn配置。我通过覆盖Ext.util.PullRefresh中的refreshFn函数,成功实现了新版Sencha Touch的自定义fetchLatest,如此...

Ext.define('MyApp.overrides.PullRefreshOverride', {
    override: 'Ext.plugin.PullRefresh',

    fetchLatest: function() {
        var list = this.getList();

        switch(list.getItemId()) {
            case "list1": 
                this.updateStore1();
                break;

            case "list2": 
                this.updateStore2();
                break;
        }

        this.callParent(arguments);
    },

    //My own custom function to add to the plugin
    updateStore1: function() {
        //Code to update store 1
    },

    //My own custom function to add to the plugin
    updateStore2: function {
        //Code to update store 2
    }
});

答案 1 :(得分:2)

看一下 sencha-touch-all-debug 中的Ext.plugin.PullRefresh定义,我看到了这个配置:

    /*
     * @cfg {Function} refreshFn The function that will be called to refresh the list.
     * If this is not defined, the store's load function will be called.
     * The refresh function gets called with a reference to this plugin instance.
     * @accessor
     */
    refreshFn: null,

您可以通过refreshFn配置实现所需的功能。

答案 2 :(得分:1)

对于需要refreshFn返回的用户,PullRefresh有一个PullRefreshFn扩展名。

我需要PullRefresh由Panel触发,而不是List或Dataview,我还需要在用户触发PullRefresh时手动加载并设置数据到我的Dataview。

为此我需要在Sencha 2.2之前存在的refreshFn配置函数,所以这是我的实现。


PullRefreshFn (已修改)

Ext.define('Ext.plugin.PullRefreshFn', {
    extend: 'Ext.plugin.PullRefresh',
    alias: 'plugin.pullrefreshfn',
    requires: ['Ext.DateExtras'],

    config: {
        /**
         * @cfg {Function} refreshFn The function that will be called to refresh the list.
         * If this is not defined, the store's load function will be called.
         */
        refreshFn: null
    },

    fetchLatest: function() {
        if (this.getRefreshFn()) {
            this.getRefreshFn().call();
        } else {
            var store = this.getList().getStore(),
                proxy = store.getProxy(),
                operation;

            operation = Ext.create('Ext.data.Operation', {
                page: 1,
                start: 0,
                model: store.getModel(),
                limit: store.getPageSize(),
                action: 'read',
                sorters: store.getSorters(),
                filters: store.getRemoteFilter() ? store.getFilters() : []
            });

            proxy.read(operation, this.onLatestFetched, this);
        }
    }

});

我的控制器

Ext.define('myApp.controller.MyController', {
    extend: 'Ext.app.Controller',
    requires: ['Ext.plugin.PullRefreshFn'],

    ...

    // More code

    ...

    // Binds the Pull Refresh to myPanel view item.
    // myPanel is a panel. Not list nor dataview.
    setPullRefresh: function () {
        var me = this;

        // We get reference to myPanel and
        // we set PullRefreshFn
        this.getMyPanel().setPlugins([{
            xclass: 'Ext.plugin.PullRefreshFn',
            docked: 'top',

            // We set autoSnapBack to false,
            // as we are going to trigger this manually
            autoSnapBack: false,

            // refreshFn will be called upon user releasing for refresh.
            refreshFn: function() {

                // This is a custom function that sets data to our dataview list.
                // When it's done setting data, we trigger the snapBack.
                me.populateMylist(function () {
                    me.getMyPanel().getPlugins()[0].snapBack(true);
                });
            }
        }]);
    }

});