如何使用 MVP 将事件处理程序添加到动态添加到视图的 Widget
我有一个代表网站的面包屑的视图,我通过像这样设置面包屑来更新视图
public void setCrumb(ArrayList<Crumb> crumbs) {
content.clear();
for (int i=0;i<crumbs.size()-1; i++){
Anchor anchor = new Anchor(crumbs.get(i).getText())
content.add(anchor);
content.add(new InlineHTML(" > "));
}
content.add(crumbs.get(crumbs.size()-1).getText());
}
现在,我想将事件处理程序添加到每个添加的锚点。我可以通过执行类似 anchor.addClickHandler(...)
之类的操作在 for 循环中设置处理程序,但我使用的是 MVP,因此视图不必管理处理程序...我猜测。在演示者中,我可以访问包含所有锚点的面板。
我的问题是:如何从演示者访问锚点并设置事件处理程序?
I have a view which reprents the BreadCrumb of a website, I update the view by setting the crumbs like this
public void setCrumb(ArrayList<Crumb> crumbs) {
content.clear();
for (int i=0;i<crumbs.size()-1; i++){
Anchor anchor = new Anchor(crumbs.get(i).getText())
content.add(anchor);
content.add(new InlineHTML(" > "));
}
content.add(crumbs.get(crumbs.size()-1).getText());
}
Now, I want to add EventHandlers to every Anchor added. I could just set the handler in the for loop by doing something like anchor.addClickHandler(...)
but I'am using MVP, so the view doesn't have to manage handlers... I guess. In the presenter I have access to the panel that has all the anchors.
My question is : How to access the anchors from the presenter and set the eventHandler ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 MVP 环境中,UI 元素的创建和捕获事件应该在 View 中进行,然后 View 应该处理这些事件并调用 Presenter 来采取适当的操作,如下所示:
在 Presenter 中,您希望有一个方法 < code>handleEvent(Event event) 可以使用事件,以及调用 View 的
setCrumb(crumbs)
方法。在我看来,您将 MVP 视为与 MVC 一样。不同之处在于,在 MVC 中处理事件是控制器的工作,而在 MVP 中该工作属于视图。关于 MVP 有一些很棒的资源,您一定要查看它们。有关基础知识,您可以参阅维基百科文章:Model-View-Presenter,其中还包含对其他几个重要资源的引用。编辑:这是我个人用例中的一些代码,非常相似。这一切都发生在视图中,基于演示者提供的
regionReportElement
。In an MVP environment, creating of UI elements and catching events should happen in the View and the View should then handle those events and call to the Presenter to take appropriate action, like so:
And in the Presenter, you want to have a method
handleEvent(Event event)
that can consume the events, and a method that calls to the View tosetCrumb(crumbs)
. It sounds to me like you're looking at MVP as the same as MVC. The difference is that handling events is the job of the Controller in MVC, while that job belongs to the View in MVP. There are some great resources out there on MVP, and you should definitely check them out. For basics, you can see the Wikipedia Article: Model-View-Presenter, which also contains references to several other great resources.EDIT: here's some code from my personal use case, which is quite similar. This all happens in the View, based on a
regionReportElement
provided by the Presenter.