简体   繁体   English

ExtJS中的控制器职责是什么? Ext.window.Window实例中的事件?

[英]What is the controller responsibility in ExtJS? Events in instance of Ext.window.Window?

Currently using ExtJS 4 and I am trying to use the controller for implementing all events but there are some events that i am handling in the Window itself which are the close and destroy events. 当前正在使用ExtJS 4,我正在尝试使用控制器来实现所有事件,但是在Window本身中要处理的一些事件是close和destroy事件。 I need to execute some code here. 我需要在这里执行一些代码。

Is this bad practice ? 这是不好的做法吗? If the events happen in the Window (an instance of Ext.window.Window). 如果事件在Window中发生(Ext.window.Window的实例)。

Should I but forwarding these events to the controller to handle ? 我是否应该将这些事件转发给控制器进行处理?

I am unsure the correct way of doing this but I presume I would have to get a reference to the controller from my "window" in it's event and then call fireEvent on the controller? 我不确定执行此操作的正确方法,但是我想我必须从事件的“窗口”中获取对控制器的引用,然后在控制器上调用fireEvent?

What is the best practice here? 最佳做法是什么?

I am using ExtJS 4.2 so cannot use MVVM. 我正在使用ExtJS 4.2,因此无法使用MVVM。

ExtJS seems to let me implement the events directly in "Components" but following the MVC pattern, is this not bad practice and everything should really pass through the controller. ExtJS似乎可以让我直接在“组件”中实现事件,但是遵循MVC模式,这不是一个坏习惯,并且所有内容都应该真正通过控制器。

The controller or viewport should handle the event. 控制器或视口应处理事件。 You don't need a reference to the controller inside the window. 您不需要在窗口内引用控制器。

If you want custom events, you need to fire the event from inside the window like: 如果要自定义事件,则需要从窗口内部触发事件,例如:

this.fireEvent("myCustomEvent"[,elementThatTriggeredTheEvent]);

Then you then need to listen to the event inside the controller using a reference to the window: 然后,您需要使用对窗口的引用来监听控制器内的事件:

yourWindow.on("myCustomEvent",this.myCustomEventHandler[,scope]);

There are also ways to listen to events without having a reference to the event triggering element. 还有一些无需引用事件触发元素即可侦听事件的方法。

Edit- Regarding your comment: 编辑-关于您的评论:

If you need to do stuff on afterlayout regarding elements inside the window, then i would do it inside the window. 如果您需要对窗口内的元素进行事后布局方面的工作,那么我将在窗口内进行。 If you want to do stuff to elements that are not defined inside the window, then let the controller, that instantiated the elements, handle it. 如果要对窗口内未定义的元素进行处理,则让实例化元素的控制器处理它。 In ExtJS 5 there is something called a ViewController. 在ExtJS 5中,有一个叫做ViewController的东西。 I don't know if it's already in 4.2. 我不知道它是否已经在4.2中。 You could utilize the ViewController to split the stuff that happens in your window. 您可以利用ViewController拆分窗口中发生的事情。 Here is a link on that: 这是一个链接:

https://www.sencha.com/blog/using-viewcontrollers-in-ext-js-5/ https://www.sencha.com/blog/using-viewcontrollers-in-ext-js-5/

EDIT 2: 编辑2:

If you don't want custom events, just listen with the controller to the events the window already throws on important occasions. 如果您不希望自定义事件,只需与控制器一起监听在重要场合窗口已经抛出的事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM