ExtJS 4 MVC视图的多个实例和子/子控制器的困难 [英] ExtJS 4 MVC multiple instances of views and sub/child controller difficulties

查看:95
本文介绍了ExtJS 4 MVC视图的多个实例和子/子控制器的困难的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ExtJS 4中遇到了MVC模式问题.至少,我认为我有.我已经与多个人接触了这个问题,并在Sencha论坛上发表了很多次,现在我转向更广泛的受众,希望得到一个灯泡或一个确认.

I have encountered a problem with the MVC pattern in ExtJS 4. At least, I think I have. Having approached multiple people with this question, and having posted numerous times in the Sencha forums, I am now turning to a broader audience in hopes of getting either a light bulb or a confirmation.

问题

您的应用程序可以打开许多不同的视图,其中一些视图本身是微型应用程序.此外,用户可能希望打开一个视图的多个并发副本.

Your application has the ability to open many different views, some of which themselves are mini-applications. Additionally, a user may wish to have multiple concurrent copies of a view open.

此应用程序是单页客户端Javascript应用程序.

This application is a single-page client-side Javascript application.

ExtJS 4 MVC模型期望您在Application类中定义所有控制器.然后在应用程序加载时初始化这些控制器.控制器跟踪视图,模型和存储.

The ExtJS 4 MVC model expects you to define all of your controllers in your Application class. These controllers are then initialized when the Application loads. Controllers keep track of views, models and stores.

多次初始化控制器A(例如,创建一个视图的多个副本)时,最终会得到两个引用相同数据存储的视图,并在功能上将重复事件发送到应用程序事件总线.

When you initialize controller A multiple times, say to create more than one copy of a view, you end up with two views that reference the same data stores, and functionally send duplicate events to the Application event bus.

我通过向Component和Controller添加新的原型方法来重构我的应用程序,以允许a)子控制器(一些控制器变得非常庞大)和b)专门为其使用的视图定义存储.仍然可以在控制器上定义模型,只是为了便于处理程序使用(如果您需要执行一些操作,例如从服务器获取记录).

I have refactored my application by adding new prototype methods to Component and Controller to allow for both a) sub controllers (some of my controllers were getting pretty huge) and b) defining stores specifically for the view they work with. The models can still be defined on the controller, just for ease of use by handlers if you need to do something like grab a record from the server.

问题

我对MVC的理解使我相信,与控制器相比,模型与View更直接相关. 我假设ExtJS 4决定将存储(我认为可以将其包装为更经典的模型的包装)附加到Controllers,以鼓励重复使用已加载的数据,并从拥有多个存储副本的角度进行优化实例化相同的类.但是,在我看来,如果一个人打算让用户可以使用许多视图实例,则无法做到这一点.在我看来,拥有很多实例是OO框架中的重要选择,因此为什么我逆势而上,并在某些Ext基类上实现了原型. (感谢Ext.implement!).

My understanding of MVC would lead me to believe that models more directly relate to the View than then Controller. I asssssume that ExtJS 4 decides to attach stores (which I think can be seen as wrappers to a more classic model) to Controllers for purposes of encouraging re-use of loaded data, and to optimize away from having many copies of the same class instantiated. It seems to me, however, that one cannot do this if one intends to have many instances of a view available to the user. To my thinking, having many instances is an important option in an OO framework, hence why I have bucked the trend and implemented prototypes on some of the Ext base classes. (Thank you Ext.implement!).

是否有任何方法可以使用现成的MVC类并利用提供的setter,getters等,使视图中的多个并发实例具有不同的数据加载?

Is there any way to have multiple concurrent instances of a view with different data loaded into them using the out of the box MVC classes and making uses of the provided setters, getters, etc?

推荐答案

我遇到了类似的问题:

考虑CRM类型应用程序的选项卡,该选项卡为每个客户端打开视图的新实例.并说该选项卡视图包含3或4个行编辑网格面板,用于与与该客户端有关的不同数据集合进行交互.

Consider a tabpanel for a CRM type application which opens new instances of a view for each client. And say that tab view contains 3 or 4 row-editing gridpanels for interacting with different collections of data relating to that client.

我想出的解决方案基于> .在坚果壳中,几乎所有从视图调度的事件都包含对视图本身的引用.控制器控制功能中的处理程序都使用这些处理程序来获取对正确视图实例的引用.

The solution I came up with was based on this from the Sencha forums. In a nut shell, almost all events that are dispatched from a view contain a reference to the view itself. The handlers in my controller's control function all use these to get a reference to the correct view instance.

对于处理同一商店所需的多个实例,我从那篇博文中牢记了这一点:

For dealing with the multiple instances of the same store needed for this, I took this to heart from that post:

对于视图或全局视图上的Store实例...取决于 需求.如果要全局使用,请使其全局使用.如果你 只需要在视图上使用它,然后将其放在视图上. MVC是 不是法律,您可以根据自己的需要进行更改.从技术上讲 MVC的控制器部分假设是中间人. 查看和建模零件,但有时并不需要.我创造 在视图中有95%的时间在商店中.我给你举个例子...

For the Store instance on the view or a global one... depends on the needs. If you are going to use globally then make it global. If you only are going to need it on the view then put it on the view. MVC is not a law, you can change it to fit your needs. Technically the Controller part of MVC is suppose to be the middle man between the View and Model parts but sometimes that's just not needed. I create the Store in the view 95% of the time. I'll give you an example...

如果您有一家产品商店,则可能只需要参考 该存储在您的网格中.通常不需要 应用程序.但是,如果您有商店可以加载国家/地区, 通常在全球范围内需要它,所以我只需要加载一次即可 在多个视图中设置/使用该商店.

If you have a Store for products, you probably only need to reference that Store in your Grid. That usually isn't needed for other parts of the application. However, if you have a Store to load countries, I often need it globally so I only have to load it once and can then set/use that Store in several views.

因此,我刚刚在视图的initComponent方法内部创建了与视图实例特别相关的所需存储.该应用程序确实遵循MVC建议,创建了一些作为商店类的全局商店.很好地将视图实例存储封装在视图内部.然后,我只需要一个控制器实例.

So I just created the needed store's that relate to a view instance specifically, inside the view's initComponent method. The application did have a few global stores that I created as store classes following the MVC recommendations. It worked out nicely to encapsulate the view instance stores inside the view. Then I only needed one instance of the controller.

要专门回答您的问题,当前,没有ExtJS官方建议或配置来处理使用同一商店构造函数的同一视图的多个实例.我花了一些时间寻找类似的东西,而我发现最好的是来自他们的一位论坛主持人的推荐.

To answer your question specifically, currently, there is no ExtJS official recommendation or config for dealing with multiple instances of the same view that use the same store constructor. I have spent some time looking for something like that and the best I have found was this recommendation from one of their forum moderators.

这篇关于ExtJS 4 MVC视图的多个实例和子/子控制器的困难的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆