Sencha Touch 2 MVC - 如何使用按钮切换视图
我有这个控制器:
Ext.define('MyApp.controller.Test', {
extend: 'Ext.app.Controller',
config: {
},
refs: [
{
ref: 'first',
selector: '#first'
},
{
ref: 'second',
selector: '#second'
}
],
views : [
'TestMain',
'TestSecond'
],
init: function() {
this.getTestMainView().create();
this.control({
'#first': {
tap: function() {
//how do I go to second view here?
}
},
'#second': {
tap: function() {
}
}
});
}
});
以及这两个视图:
Ext.define('MyApp.view.TestMain', {
extend: 'Ext.Container',
xtype: 'testmain',
config: {
fullscreen: true,
layout: 'vbox',
scrollable: true,
items: [
{
xtype: 'button',
ui: 'normal',
id: 'first',
text: 'Go To Second Screen',
handler: function() {
//how do I go to second view here?
}
}
]
}
});
...
Ext.define('MyApp.view.TestSecond', {
extend: 'Ext.Container',
xtype: 'testsecond',
config: {
fullscreen: true,
layout: 'vbox',
scrollable: true,
items: [
{
xtype: 'button',
ui: 'normal',
id: 'second',
text: 'Go To First Screen',
handler: function() {
}
}
]
}
});
我希望在单击第一个按钮时加载第二个视图,反之亦然,当我单击第二个按钮时加载第二个视图。看来我可以在按钮处理程序或控制部分中添加代码 - 我希望能提供两者的示例(除非它们相同),并且可能解释哪种方法最好以及原因。
请注意,我不想使用卡片布局或选项卡面板 - 我想知道如何从一个独立视图切换到另一个独立视图(在我的应用程序中,我有一个卡片面板和一个选项卡面板,我需要使用按钮在两个组之间切换)
谢谢!!
I have this controller:
Ext.define('MyApp.controller.Test', {
extend: 'Ext.app.Controller',
config: {
},
refs: [
{
ref: 'first',
selector: '#first'
},
{
ref: 'second',
selector: '#second'
}
],
views : [
'TestMain',
'TestSecond'
],
init: function() {
this.getTestMainView().create();
this.control({
'#first': {
tap: function() {
//how do I go to second view here?
}
},
'#second': {
tap: function() {
}
}
});
}
});
and these 2 views:
Ext.define('MyApp.view.TestMain', {
extend: 'Ext.Container',
xtype: 'testmain',
config: {
fullscreen: true,
layout: 'vbox',
scrollable: true,
items: [
{
xtype: 'button',
ui: 'normal',
id: 'first',
text: 'Go To Second Screen',
handler: function() {
//how do I go to second view here?
}
}
]
}
});
...
Ext.define('MyApp.view.TestSecond', {
extend: 'Ext.Container',
xtype: 'testsecond',
config: {
fullscreen: true,
layout: 'vbox',
scrollable: true,
items: [
{
xtype: 'button',
ui: 'normal',
id: 'second',
text: 'Go To First Screen',
handler: function() {
}
}
]
}
});
I would like the second view to load when I click on first button and vice versa when I click on second button. It seems I can add code either in my button handler or in the control section - I would appreciate an example of both (unless they are the same) and maybe an explanation as to which method is best and why.
Note that I do NOT want to use card layout or tabpanel - I want to know how to switch from one standalone view to another (In my app I have a card panel and a tab pabel and I need to switch between both groups using buttons)
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该在控制器中使用
this.control
而不是处理程序:(您可以从视图定义中的按钮中删除
handlers:
)此外,您可能应该'首先不要对 id 进行硬编码。
我处理这样的按钮的方式是:
然后在适用于所有视图的基本控制器中,我执行以下操作:
Instead of the handlers, you should use
this.control
in the controller:(you can drop the
handlers:
from the button in the view definition)Also, you probably shouldn't hard-code ids in the first place.
The way I handle buttons like this is:
Then in a base controller that applies to all views, I do something like:
很好的答案,但请注意,仅使用 xtype 调用 setActiveItem() 将创建新视图,而不是重用已创建的视图,我最终使用 Ext.ComponentQuery 首先获取目标,然后 setActiveItem 另外请注意,我使用自定义视口
来添加幻灯片效果等,但像前面的示例一样直接调用 Ext.Viewport.setActiveItem() 也可以工作。
Excellent answer but beware that calling setActiveItem() with just an xtype will create new views instead of reusing the ones that are already created, I ended up using
Ext.ComponentQuery
to get the target first and then setActiveItem on that, here's the code:Also note that I use a custom viewport to add slide effects and such, but calling directly
Ext.Viewport.setActiveItem()
like the previous example will work as well.首先我们需要检查该 xtype 是否已经创建。下面的代码将做到这一点
First we need to check if that xtype is already created or not. Following code will do that
这对我有用:
This worked for me: