0

我已经定义了一个 Ext.navigation.View,我需要将一个事件处理程序附加到我的代码中指定的元素,如下所示:

title: 'Timetable XXX', 
            iconMask: true,
            iconCls: 'XXX-icon-timetable'

知道怎么做吗?

Ext.define('XXX.view.NavigViewTimetable', {
        extend: 'Ext.navigation.View',

        alias: 'widget.navigviewtimetable',

        requires: [
        'XXX.view.TimetableContainer',
        'XXX.view.DetailView',
        'XXX.view.TimetableEdit',
        'XXX.view.TimetableDayList'
        ],

        config: {      
            navigationBar: {
                items: [
                    {
                        xtype: 'button',
                        text: 'Add',
                        itemId: 'addButton',
                        ui: 'custom-btn-up-timetable',
                        align: 'right',
                        hidden: false
                    },

                ],
                ui: 'custom-toolbar-top-1L'
            },

            items: [
                {
                    xtype: 'timetablecontainer'
                },

                //Toolbar
                {
                    xtype: "toolbar",
                    docked: "bottom",
                    ui: 'custom-toolbar-2L',

                    items: [
                        {
                            xtype: "button",
                            text: 'Today',
                            ui: 'custom-btn-dwn-timetable',
                            //handler: this.onTodayButtonTap,
                            //scope: this,
                            itemId: 'todayButton'
                        },
                        {
                            xtype: 'spacer'
                        },
                        {
                            xtype: "segmentedbutton",
                            items: [
                            { 
                                text: 'Daily',
                                ui: 'custom-btn-dwn-timetable'      
                            },
                            { 
                                text: 'List',
                                ui: 'custom-btn-dwn-timetable',
                                //disabled: true,
                                pressed: true       
                            }
                            ],

                            itemId: 'segBtn',   
                            align: 'right'
                        }
                    ]
                }

            ],

            listeners: [
                {
                    delegate: '#addButton',
                    event: 'tap',
                    fn: 'onAddButtonTap'
                },
                {
                    delegate: '#todayButton',
                    event: 'tap',
                    fn: 'onTodayButtonTap'
                },
                {
                    delegate: '#segBtn',
                    event: 'toggle',
                    fn: 'onSegBtnToggle'
                }

            ], 

            // I NEED ADD LISTEN TO AN EVENT HERE
            title: 'Timetable XXX', 
            iconMask: true,
            iconCls: 'XXX-icon-timetable' 


        },

        onAddButtonTap: function () {
            console.log("addItemCommand [NavigViewTimetable]");
            this.fireEvent("addItemCommand", this);
        },

        onTodayButtonTap: function(){
            console.log('onTodayButtonTap [NavigViewTimetable]');
            this.fireEvent('todayButtonTapCommand', this)
        },
        onSegBtnToggle: function (segBtn, btn, isPressed) {
            //console.log("'" + btn.config.text +"' on segmented button pressed" );
            if (btn.config.text == 'List'){
                this.fireEvent('listToggleCommand', this);
            }else if (btn.config.text == 'Daily'){
                this.fireEvent('dailyToggleCommand', this);


            }                         
        }
    });
4

1 回答 1

3

与其将与事件处理程序相关的代码放在视图中,我更喜欢使用Controllers. 这肯定会减少维护代码的工作量。另一件事,如果您决定使用控制器,那么您可以action为每个按钮使用配置。喜欢 -

                    xtype: 'button',
                    text: 'Add',
                    itemId: 'addButton',
                    ui: 'custom-btn-up-timetable',
                    align: 'right',
                    hidden: false,
                    action:'addButtonAction'

然后在控制器中,您可以使用control配置选项。控制器的 Ex 可以是 -

Ext.define('XXX.controller.ButtonController',{
     extend:'Ext.app.Controller',
     config:{
          refs:{
               views:['Theviews.youwanto.refer'],
               //addtional references  
          },
          control:{
               'button[action=addButtonAction]':{
                     tap:'functionForAddButton'
                }
          }        
     },
     functionForAddButton:function(){
          console.log('Add Button tapped');
     } 

);

您还可以action在分段按钮上使用配置 -

                        xtype: "segmentedbutton",
                        items: [
                        { 
                            text: 'Daily',
                            ui: 'custom-btn-dwn-timetable',
                            action:'dailyButtonAction'      
                        },
                        { 
                            text: 'List',
                            ui: 'custom-btn-dwn-timetable',
                            //disabled: true,
                            pressed: true,
                            action:'listButtonAction'       
                        }
                        ],

您始终可以在视图中定义事件处理程序,但 IMO 不遵循 MVC 主体。

于 2013-02-12T13:57:26.210 回答