ExtJS控制器在超链接点击时触发
我已经尝试让它工作很长一段时间了,但除了在视图中的侦听器内部添加后渲染之外,我仍然无法找到解决方案。但我希望控制器能够处理它。
有谁知道我该如何解决这个问题?
这就是我目前的文件中的内容 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您尝试引用超链接
a
的方式将不起作用。这只会查找 ExtJS 组件,而不是 DOM 元素。您很可能必须在渲染后或类似的地方附加点击。这并不意味着您不能将完成所有工作的方法留在控制器中。
编辑:
The way you are trying to reference the hyperlink
a
won't work.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:
我也曾在问题上挣扎过。最重要的是,控制器控制配置不像常规侦听器那样工作,并且只会作用于由组件触发的事件,而不是像 s_hewitt 指出的那样由 DOM 触发。
替代方案有点难看,本质上是组件或父组件上的侦听器,您也可以在其中使用委托。一旦你走这条路,它就不像在控制器中设置监听器那么清晰了。
我用来解决此限制的另一个技巧是从视图组件调用控制器方法,如下所示:
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: