ExtJS控制器在超链接点击时触发

发布于 2024-12-12 05:02:58 字数 1174 浏览 0 评论 0原文

我已经尝试让它工作很长一段时间了,但除了在视图中的侦听器内部添加后渲染之外,我仍然无法找到解决方案。但我希望控制器能够处理它。

有谁知道我该如何解决这个问题?

这就是我目前的文件中的内容 http://pastie.org/2751446

controller/App.js

    Ext.define('HD.controller.App', {
     extend: 'Ext.app.Controller'
     ,views: [
         'core.Header',
         'core.Navigation',
         'core.Content'
     ]
     ,init: function() {
         this.control({
             'navigation a[id=tab1]': {
                 click: this.newTab
             }
         })
     }
     ,newTab: function() {
         console.log('Tab 1 should be loaded now');
     }
});

view/core/Navigation.js

Ext.define('HD.view.core.Navigation', {
     extend: 'Ext.panel.Panel'
    ,name: 'navigation'
    ,alias: 'widget.navigation'
    ,layout: 'accordion'
    ,region: 'west'
    ,width: 200

    ,title: 'Navigation'
    ,collapsible: true

    ,items: [
        {
            title: 'Title 1'
            ,html: '<a id="tab1" style="cursor:pointer;">Tab 1</a>'
        },
        {
            title: 'Title 2'
            ,html: 'Second'
        }
    ]
});

I've been trying to get this to work for quite a while now but I still haven't been able to find a solution for this except adding a afterrender inside listeners in the view. But I want the controller to handle it.

Does anyone have an idea on how I can solve this?

This is what I have in my files at the moment
http://pastie.org/2751446

controller/App.js

    Ext.define('HD.controller.App', {
     extend: 'Ext.app.Controller'
     ,views: [
         'core.Header',
         'core.Navigation',
         'core.Content'
     ]
     ,init: function() {
         this.control({
             'navigation a[id=tab1]': {
                 click: this.newTab
             }
         })
     }
     ,newTab: function() {
         console.log('Tab 1 should be loaded now');
     }
});

view/core/Navigation.js

Ext.define('HD.view.core.Navigation', {
     extend: 'Ext.panel.Panel'
    ,name: 'navigation'
    ,alias: 'widget.navigation'
    ,layout: 'accordion'
    ,region: 'west'
    ,width: 200

    ,title: 'Navigation'
    ,collapsible: true

    ,items: [
        {
            title: 'Title 1'
            ,html: '<a id="tab1" style="cursor:pointer;">Tab 1</a>'
        },
        {
            title: 'Title 2'
            ,html: 'Second'
        }
    ]
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁月苍老的讽刺 2024-12-19 05:02:58

您尝试引用超链接 a 的方式将不起作用。

this.control({
   'navigation a[id=tab1]': {
      click: this.newTab
   }
})

这只会查找 ExtJS 组件,而不是 DOM 元素。您很可能必须在渲染后或类似的地方附加点击。这并不意味着您不能将完成所有工作的方法留在控制器中。

编辑:

var controller = this;
controller.control({
   'navigation': {
      afterrender: function() {
           Ext.get('tab1').on('click', function() {
                controller.newTab();
           });
      }
   }
})

The way you are trying to reference the hyperlink a won't work.

this.control({
   'navigation a[id=tab1]': {
      click: this.newTab
   }
})

That will only look for ExtJS components, not DOM elements. You will most likely have to attach the click in afterrender or somewhere similar. This does not mean you can't leave the methods that do all the work in the controller.

Edit:

var controller = this;
controller.control({
   'navigation': {
      afterrender: function() {
           Ext.get('tab1').on('click', function() {
                controller.newTab();
           });
      }
   }
})
一张白纸 2024-12-19 05:02:58

我也曾在问题上挣扎过。最重要的是,控制器控制配置不像常规侦听器那样工作,并且只会作用于由组件触发的事件,而不是像 s_hewitt 指出的那样由 DOM 触发。

替代方案有点难看,本质上是组件或父组件上的侦听器,您也可以在其中使用委托。一旦你走这条路,它就不像在控制器中设置监听器那么清晰了。

我用来解决此限制的另一个技巧是从视图组件调用控制器方法,如下所示:

MyApp.app.getController('MyController').myFunction();

I have struggled with issue as well. The bottom line is the Controller control config does not work like the regular listeners and will only act on the events which are fired by the component and not by DOM as s_hewitt pointed out.

The alternatives are somewhat ugly with essentially listeners on the components or parent components where you can use delegates as well. Once you go that route it's not as clean cut as setting up listeners in the controller.

Another trick I used to get around this limitation is calling controller methods from the view components like this:

MyApp.app.getController('MyController').myFunction();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文