全局视图+;ExtJs 5上的ViewController
我想要实现的是非常简单的,我希望在我的应用程序中都有一个主菜单,所有视图都有相同的功能 我想创建一个只包含菜单部分和它自己的viewcontroller的视图。实现这一目标的最佳途径是什么 我正在使用ExtJS5实现MVVM范式全局视图+;ExtJs 5上的ViewController,extjs,mvvm,extjs5,Extjs,Mvvm,Extjs5,我想要实现的是非常简单的,我希望在我的应用程序中都有一个主菜单,所有视图都有相同的功能 我想创建一个只包含菜单部分和它自己的viewcontroller的视图。实现这一目标的最佳途径是什么 我正在使用ExtJS5实现MVVM范式 谢谢 这是一个关于如何构建应用程序的相当广泛的问题,如果不了解应用程序的其他部分,就很难回答这个问题 通常,任何应用程序全局(不是应用程序容器/视口)都可能最容易用MVC实现。菜单控制器(MVC控制器)将侦听菜单事件,然后它将深入组件层次结构以调用组件的API方法来执行
谢谢 这是一个关于如何构建应用程序的相当广泛的问题,如果不了解应用程序的其他部分,就很难回答这个问题 通常,任何应用程序全局(不是应用程序容器/视口)都可能最容易用MVC实现。菜单控制器(MVC控制器)将侦听菜单事件,然后它将深入组件层次结构以调用组件的API方法来执行操作
如果我了解这个应用程序,我可能会说得更具体。这是一个关于如何构建应用程序的相当广泛的问题,如果不了解应用程序的其他部分,就很难回答这个问题 通常,任何应用程序全局(不是应用程序容器/视口)都可能最容易用MVC实现。菜单控制器(MVC控制器)将侦听菜单事件,然后它将深入组件层次结构以调用组件的API方法来执行操作
如果我了解这个应用程序,我可能会更具体。我会创建一个主视图,在其中定义应用程序的固定部分,以及一个布局为“适合”的容器,以容纳不断变化的“视图”。这也可以是一个选项卡面板或其他东西,而不是布局“适合”。当然,没有什么可以阻止您使用视图控制器将行为添加到此主视图的固定部分 事实上,这很简单。然后,您将通过将当前应用程序视图放入主视图的中央容器来更改它。您需要某种决策逻辑和配置数据来定义应用程序中的可用视图。这可能最好将其包装在一个专门用于此任务的地方,即应用程序控制器(而不是视图控制器) 下面是一个例子,下面是解释代码不同部分的推理: 因此,您可以从这样的视图开始:
Ext.define('My.view.Main', {
extend: 'Ext.container.Container',
xtype: 'main', // you can do that in ext5 (like in touch)
// shortcut for that:
//alias: 'widget.main',
controller: 'main',
layout: 'border',
items: [{
xtype: 'panel',
region: 'west',
layout: {type: 'vbox', align: 'stretch', padding: 5},
defaults: {
margin: 5
},
items: [{
xtype: 'button',
text: "Say Hello",
handler: 'sayHello'
}]
},{
// target for app's current view (that may change)
xtype: 'container',
region: 'center',
layout: 'fit'
}]
});
Ext.define('My.view.MainController', {
extend: 'Ext.app.ViewController',
alias: 'controller.main',
sayHello: function() {
Ext.Msg.alert("Controller says:", "Hello :-)");
}
});
然后,将此主视图设置为应用程序的“视口”。我还添加了一个方法来更改中心视图。我认为应用程序实例是一个很好的地方,但您可以将此方法移动到另一个专用的应用程序控制器
Ext.application({
name : 'My', // app namespace
// in real life, Main view class would lie in another file,
// so you need to require it
views: ['Main'],
// from ext 5.1, this is the config to auto create main view
mainView: 'My.view.Main',
// we also register a ref for easy retrieval of the main view
// (the value 'main' is the xtype of the main view -- this is
// a component query)
refs: {
main: 'main'
},
setCenterRegion: function(cmp) {
// getter generated by refs config
// you could another layout in the main view, and another component query of course
var center = this.getMain().down('[region=center]');
// replace the current center component with the one provided
center.removeAll();
center.add(cmp);
}
});
现在,您可以使用如下代码更改视图:
My.getApplication().setCenterRegion(myView);
{
xtype: 'button',
text: "Change view (by view controller)",
handler: 'changeView'
}
Ext.define('My.controller.Main', {
extend: 'Ext.app.Controller',
config: {
control: {
// components will have to match this component query
// to be animated with the change view behaviour
'#changeView': {
click: 'changeView'
}
}
},
changeView: function() {
My.getApplication().setCenterRegion(/*
...
*/);
}
});
{
xtype: 'button',
text: "Change view (by app controller)",
// will be matched by the controller component query
itemId: 'changeView'
}
您可以通过主视图的ViewController连接它,并将其用作视图中的处理程序。例如,在ViewController中:
changeView: function() {
// retrieve the next view, this is application specific obviously
var myView = ...
// My.getApplication() gives you the instance created by
// Ext.getApplication in namespace 'My'
My.getApplication().setCenterRegion(myView);
}
在主视图中,使用如下项:
My.getApplication().setCenterRegion(myView);
{
xtype: 'button',
text: "Change view (by view controller)",
handler: 'changeView'
}
Ext.define('My.controller.Main', {
extend: 'Ext.app.Controller',
config: {
control: {
// components will have to match this component query
// to be animated with the change view behaviour
'#changeView': {
click: 'changeView'
}
}
},
changeView: function() {
My.getApplication().setCenterRegion(/*
...
*/);
}
});
{
xtype: 'button',
text: "Change view (by app controller)",
// will be matched by the controller component query
itemId: 'changeView'
}
对于简单的应用程序来说,这可能很好,但这似乎是一个混合的问题。决定应用程序级视图交换似乎更像是应用程序控制器的工作。因此,我建议将changeView
方法放在app controller中,并通过组件查询将其公开给组件,如下所示:
My.getApplication().setCenterRegion(myView);
{
xtype: 'button',
text: "Change view (by view controller)",
handler: 'changeView'
}
Ext.define('My.controller.Main', {
extend: 'Ext.app.Controller',
config: {
control: {
// components will have to match this component query
// to be animated with the change view behaviour
'#changeView': {
click: 'changeView'
}
}
},
changeView: function() {
My.getApplication().setCenterRegion(/*
...
*/);
}
});
{
xtype: 'button',
text: "Change view (by app controller)",
// will be matched by the controller component query
itemId: 'changeView'
}
您可以在任何视图中将行为与组件挂钩,如下所示:
My.getApplication().setCenterRegion(myView);
{
xtype: 'button',
text: "Change view (by view controller)",
handler: 'changeView'
}
Ext.define('My.controller.Main', {
extend: 'Ext.app.Controller',
config: {
control: {
// components will have to match this component query
// to be animated with the change view behaviour
'#changeView': {
click: 'changeView'
}
}
},
changeView: function() {
My.getApplication().setCenterRegion(/*
...
*/);
}
});
{
xtype: 'button',
text: "Change view (by app controller)",
// will be matched by the controller component query
itemId: 'changeView'
}
我将创建一个主视图,在其中定义应用程序的固定部分,以及一个布局为“适合”的容器,以容纳不断变化的“视图”。这也可以是一个选项卡面板或其他东西,而不是布局“适合”。当然,没有什么可以阻止您使用视图控制器将行为添加到此主视图的固定部分 事实上,这很简单。然后,您将通过将当前应用程序视图放入主视图的中央容器来更改它。您需要某种决策逻辑和配置数据来定义应用程序中的可用视图。这可能最好将其包装在一个专门用于此任务的地方,即应用程序控制器(而不是视图控制器) 下面是一个例子,下面是解释代码不同部分的推理: 因此,您可以从这样的视图开始:
Ext.define('My.view.Main', {
extend: 'Ext.container.Container',
xtype: 'main', // you can do that in ext5 (like in touch)
// shortcut for that:
//alias: 'widget.main',
controller: 'main',
layout: 'border',
items: [{
xtype: 'panel',
region: 'west',
layout: {type: 'vbox', align: 'stretch', padding: 5},
defaults: {
margin: 5
},
items: [{
xtype: 'button',
text: "Say Hello",
handler: 'sayHello'
}]
},{
// target for app's current view (that may change)
xtype: 'container',
region: 'center',
layout: 'fit'
}]
});
Ext.define('My.view.MainController', {
extend: 'Ext.app.ViewController',
alias: 'controller.main',
sayHello: function() {
Ext.Msg.alert("Controller says:", "Hello :-)");
}
});
然后,将此主视图设置为应用程序的“视口”。我还添加了一个方法来更改中心视图。我认为应用程序实例是一个很好的地方,但您可以将此方法移动到另一个专用的应用程序控制器
Ext.application({
name : 'My', // app namespace
// in real life, Main view class would lie in another file,
// so you need to require it
views: ['Main'],
// from ext 5.1, this is the config to auto create main view
mainView: 'My.view.Main',
// we also register a ref for easy retrieval of the main view
// (the value 'main' is the xtype of the main view -- this is
// a component query)
refs: {
main: 'main'
},
setCenterRegion: function(cmp) {
// getter generated by refs config
// you could another layout in the main view, and another component query of course
var center = this.getMain().down('[region=center]');
// replace the current center component with the one provided
center.removeAll();
center.add(cmp);
}
});
现在,您可以使用如下代码更改视图:
My.getApplication().setCenterRegion(myView);
{
xtype: 'button',
text: "Change view (by view controller)",
handler: 'changeView'
}
Ext.define('My.controller.Main', {
extend: 'Ext.app.Controller',
config: {
control: {
// components will have to match this component query
// to be animated with the change view behaviour
'#changeView': {
click: 'changeView'
}
}
},
changeView: function() {
My.getApplication().setCenterRegion(/*
...
*/);
}
});
{
xtype: 'button',
text: "Change view (by app controller)",
// will be matched by the controller component query
itemId: 'changeView'
}
您可以通过主视图的ViewController连接它,并将其用作视图中的处理程序。例如,在ViewController中:
changeView: function() {
// retrieve the next view, this is application specific obviously
var myView = ...
// My.getApplication() gives you the instance created by
// Ext.getApplication in namespace 'My'
My.getApplication().setCenterRegion(myView);
}
在主视图中,使用如下项:
My.getApplication().setCenterRegion(myView);
{
xtype: 'button',
text: "Change view (by view controller)",
handler: 'changeView'
}
Ext.define('My.controller.Main', {
extend: 'Ext.app.Controller',
config: {
control: {
// components will have to match this component query
// to be animated with the change view behaviour
'#changeView': {
click: 'changeView'
}
}
},
changeView: function() {
My.getApplication().setCenterRegion(/*
...
*/);
}
});
{
xtype: 'button',
text: "Change view (by app controller)",
// will be matched by the controller component query
itemId: 'changeView'
}
对于简单的应用程序来说,这可能很好,但这似乎是一个混合的问题。决定应用程序级视图交换似乎更像是应用程序控制器的工作。因此,我建议将changeView
方法放在app controller中,并通过组件查询将其公开给组件,如下所示:
My.getApplication().setCenterRegion(myView);
{
xtype: 'button',
text: "Change view (by view controller)",
handler: 'changeView'
}
Ext.define('My.controller.Main', {
extend: 'Ext.app.Controller',
config: {
control: {
// components will have to match this component query
// to be animated with the change view behaviour
'#changeView': {
click: 'changeView'
}
}
},
changeView: function() {
My.getApplication().setCenterRegion(/*
...
*/);
}
});
{
xtype: 'button',
text: "Change view (by app controller)",
// will be matched by the controller component query
itemId: 'changeView'
}
您可以在任何视图中将行为与组件挂钩,如下所示:
My.getApplication().setCenterRegion(myView);
{
xtype: 'button',
text: "Change view (by view controller)",
handler: 'changeView'
}
Ext.define('My.controller.Main', {
extend: 'Ext.app.Controller',
config: {
control: {
// components will have to match this component query
// to be animated with the change view behaviour
'#changeView': {
click: 'changeView'
}
}
},
changeView: function() {
My.getApplication().setCenterRegion(/*
...
*/);
}
});
{
xtype: 'button',
text: "Change view (by app controller)",
// will be matched by the controller component query
itemId: 'changeView'
}
我认为这也意味着使用基于模块的方法?因此,在主应用程序中,您可能会嵌入一个包含导航控制器的标题,但每当在导航中单击某个内容时,它都会在该代码中处理,而不是在主应用程序中处理。我认为这也可能意味着使用基于模块的方法?因此,在主应用程序中,您可以嵌入一个包含导航控制器的标题,但每当在导航中单击某个内容时,它都会在该代码中处理,而不是在主应用程序中处理。