GWT& MVP - 显示/编辑复杂对象的最佳实践?
我了解的所有 GWT / MVP 示例似乎都过于简单,无法清楚地了解有关显示和处理稍微复杂的模型对象的最佳实践。
例如,大多数示例类似于演示者,它将单击处理程序附加到视图上的几个文本框...如果单击“保存”,则调用演示者的 save() 来简单地获取更新的值,然后我们就完成了, MVP风格。但这不太现实。
例如,假设我们有:
PresenterX - 获取一个“模型”对象,假设具有未知数量的各种基元或任何
ViewX 的 任何对象 - 应该在表格中显示模型对象,和/或允许对其进行编辑/重新保存
......这听起来非常非常基本。但是,我们不知道模型对象中需要显示的字段数量。所以这可能与动态的行/列数有关。对于表来说可能没有问题......但是演示者应该如何将其提供给视图的表?作为视图理解的模型对象,或者将其分解为一堆列表......视图本质上仍然需要理解。
此外,某些字段可能是可编辑的,在我们获得模型对象之前,这些字段是未知的(例如,模型中的某些内容决定哪些字段是可编辑的)——那么谁应该负责确定哪些字段是可编辑的,哪些是不可编辑的?可能是演示者,但我们如何以 MVP 的方式反映这一点?
最后,假设视图上有一个“保存”按钮...谁的工作是找出表中所有已更改的行?
在我看来,视图要么需要更多地理解模型,要么演示者需要更多地真正理解视图——这两者都不是好的 MVP :( ...或者也许应该有一些中间对象。
我知道有对于此类内容有一些更好/更新的有用方法(编辑器/RequestFactory 等),但我正在寻找有关上述场景的建议。
All the GWT / MVP examples I've learned about seem too simplistic to give a clear view what best practises are regarding displaying and handling slightly more complex model objects.
For example, most examples are something like a presenter that attaches a click handler to a few TextBoxes on the view...if save is clicked, the presenter's save() is called which simply gets the updated values, and we're done, MVP style. That's not so realistic though.
For example, let's say we have:
PresenterX
- gets a 'model' object, let's say any object with an unknown number of various primitives or whatever
ViewX
- should show the model object in a table, and/or allows it to be edited/re-saved
...so that sounds very, very basic. But, we don't know the amount of fields in the model object that we will need to display. So that might relate to a dynamic number of rows/columns. Probably no problem for a table...but how should the presenter give this to the view's table? As the model object that the view understands, or break it down into a bunch of Lists...that the view essentially still has to understand.
Also, certain fields might be editable, of which are unknown until we get the model object (something in the model determines what fields are editable, say) -- so who should be responsible for figuring out what is editable or not? Probably the presenter, but how do we refelct that in the view, the MVP way?
Lastly, let's say there's a 'save' button on the view...who's job is it to figure out all the rows in the table which were changed?
Seems to me that the view either needs to understand the model more, or the presenter needs to really understand the view more -- neither of which are nice MVP :( ... Or perhaps there should be some intermediary object.
I know there are some nicer/newer helpful ways for this kind of stuff (Editors/RequestFactory, etc.), but I'm looking for suggestions on the above scenarious.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我了解,MVP是与MPV点连在一起的。因此,P 与两者交互,但 M 和 V 只与 P 交互。
此外,MVP 的设计目标之一是拥有可测试的 P 和 M,这意味着 V 必须可以用模拟版本替换。因此,V 不应公开任何依赖于实现的接口(例如
HasClickHandlers
而不是Button
)。现在,如果 V 应显示通用表,则应创建通用方法:
addColumn(..)
来定义列,以及addRow(..)
来添加数据。新的CellTable
非常灵活,支持动态添加列。关于变更 - V 应通知,P 应采取行动。此外,还有新的
编辑器
,恕我直言,它不太适合 MVP,但应该很容易使用。As I understand it, MVP is a line with M-P-V points. So P interacts with both, but M and V only with P.
Also, one of design objectives of MVP is to have a testable P and M, which means that V must be replaceable with a mock version. So, V should not expose any implementation-dependent interfaces (e.g.
HasClickHandlers
instead ofButton
).Now, if V should show generic table, you should create generic methods:
addColumn(..)
to define columns andaddRow(..)
to add data. The newCellTable
is pretty flexible and supports adding columns dynamically.About changes - V should notify, P should act. Also, there are the new
Editors
, which IMHO do not fit nicely into MVP, but are supposed to be easy to use.