【问题标题】:How to perform View-Controller separation when using an "actioncolumn" (Ext.grid.column.Action)使用“actioncolumn”(Ext.grid.column.Action)时如何执行 View-Controller 分离
【发布时间】:2012-10-03 20:50:00
【问题描述】:

在 ExtJS 4 中,我有一个包含操作列的网格。每当触发该操作时,我都想执行“我的操作”。

如果没有 MVC,这将如下所示:

        /* ... */
        {
            xtype: 'gridpanel',
            columns: [
                /* ... */
                {
                    xtype: 'actioncolumn',
                    items: [{
                        handler: function(grid, rowIndex, colIndex) {
                            // my action
                        }
                    }]
                }
            ]
        }

现在我要介绍 View-Controller 分离。所以我必须将处理程序从视图移动到控制器。

但是控制器如何将其方法注册到操作列?查看ExtJS 4.1 actioncolumn docs,我找不到任何可以收听的事件。之后我也找不到设置操作列处理程序的方法。

那么如何在使用 actioncolumn 时实现清晰的 View-Controller 分离?

动作列还没有为 MVC 做好准备吗?

【问题讨论】:

    标签: extjs4 extjs-mvc


    【解决方案1】:

    问题不在于 actioncolumn,而在于它的项目不是 ExtJs 小部件。这些项目是简单的图像。这就是为什么我们不能以这种方式在control 中分配处理程序:

    this.control({
        'mygrid actioncolumn button[type=edit]' : this.onEdit
    

    然而,这种方式将是最好的。

    不幸的是,这种方式是不可能的。但是还有另一种方式,几乎和首选方式一样干净:让 actioncolumn 处理程序触发网格的自定义事件(由您创建):

    // view
    {
        xtype: 'actioncolumn',
        items: [{
            icon: 'http://cdn.sencha.io/ext-4.1.0-gpl/examples/shared/icons/fam/cog_edit.png',
            tooltip: 'Edit',
            handler: function(grid, rowIndex, colIndex) {
                // fire custom event "itemeditbuttonclick"
                this.up('grid').fireEvent('itemeditbuttonclick', grid, rowIndex, colIndex);
            }},
    
    // controller
    init: function() {
        this.control({
            'viewport > testpanel': {
                itemeditbuttonclick: this.onEdit,
                itemdeletebuttonclick: this.onDelete
            }
        });
    },
    

    示例

    这里是demo

    【讨论】:

      【解决方案2】:

      如果您的网格中恰好有一种 actioncolumn 项目,则这篇文章解释了一种比公认答案更简单的方法:

      http://mitchellsimoens.com/actioncolumn-and-mvc/

      基本上:只需在控制器中监听actioncolumnclick 事件。但是,如果您需要区分多个 actioncolumn 类型,这将不起作用。

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-18
      • 1970-01-01
      • 2023-03-18
      • 2012-07-17
      • 1970-01-01
      相关资源
      最近更新 更多