将事件放在模型上还是控制器上?

发布于 2024-12-19 01:43:50 字数 633 浏览 5 评论 0原文

这是针对使用被动视图的客户端 MVP 程序。

我想允许用户创建联系人并将其添加到报价中。我希望在创建联系人时收到报价通知。

做 a) 还是 b) 更好?

a)监听模型

将联系人传递给联系人控制器,并监听模型上的保存事件

var contact = new Contact()
contact.on('saved', function(contact){ do some stuff })

contactsController.create(contact)

contactsController,然后将联系人加载到视图中,用户输入一些信息,点击保存,联系人被保存到服务器,contact.saved事件被触发

b) 监听控制器

contactsController.on('saved', function(contact) { do some stuff })
contactsController.create()

contactsContoller,然后创建联系人模型,将联系人加载到视图中,用户输入一些信息,点击保存,联系人被保存到服务器,contactsController.saved 事件被触发

谢谢!

This is for a client-side MVP program, using Passive View.

I want to allow the user to create a contact and add it to a quote. I want the quote to be notified when the contact is created.

Is it better to do a) or b)?

a) Listen to the model

Pass the contact to the contacts controller, and listen for a saved event on the model

var contact = new Contact()
contact.on('saved', function(contact){ do some stuff })

contactsController.create(contact)

contactsController then loads the contact into the view, user enters some info, hits save, contact is saved to the server, contact.saved event is fired

b) Listen to the controller

contactsController.on('saved', function(contact) { do some stuff })
contactsController.create()

contactsContoller then creates contact model, loads the contact into the view, user enters some info, hits save, contact is saved to the server, contactsController.saved event is fired

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

墨洒年华 2024-12-26 01:43:50

我认为这取决于想要知道联系人何时保存到数据库与想要知道用户何时提交保存联系人页面之间微妙但重要的区别。即,控制器可能不是保存联系人的唯一地方。如果您想了解前者,请使用模型。如果您想了解后者,请使用控制器。

I think it depends on the subtle but important difference between wanting to know when a contact gets saved to the database versus wanting to know when a user submits the save contact page. I.e., the controller may not be the only place that saves a contact. If you want to know the former, use the model. If you want to know the latter, use the controller.

自此以后,行同陌路 2024-12-26 01:43:50

我会推荐以下内容。

  • 用户单击添加联系人
  • 视图告诉演示者已添加联系人 演示
  • 作为处理添加联系人的命令执行

该命令执行以下操作

  • 显示允许用户添加联系人的对话框或视图。
  • 命令等待,直到用户单击“添加”或“确定”以确认新联系人。
  • 然后命令更新模型。
  • 命令告诉正确的演示者*已添加联系人。

然后在处理查询的 Presenter 中,更新联系人调用将查找所有相关查询并更新联系信息。此更新可以通过侦听器模式来完成,其中所有查询视图都向呈现器注册自己。

*正确的演示者是您设置来处理显示查询的视图的任何演示者。某些系统可能具有用于显示查询的唯一演示器,而在其他系统中,它可能只是演示器的一部分,其中查询显示只是较大显示器的一部分。

I would recommend the following.

  • User clicks Add a Contact
  • The View tells the Presenter that a Contact as been added
  • The Presents execute as Command that handles adding a contact

The Command does the following

  • Displays a Dialog or View that allows the user to add the contacts.
  • The Command waits until the user clicks add or OK to confirm the new contact
  • The Command then updates the Model
  • The Command tells the right Presenter* that a contact has been added.

Then in the Presenter that handles the Queries, the Update Contact call will find all the relevant Queries and update the contact info. This update may be done through a listener pattern where all the query views register themselves with the presenter.

*The right presenter is whatever presenter you setup to handle the view that displays the queries. Some system could have a unique presenter for displaying queries, in other it may be just be a part of a presenter where the query display is just a section of a larger display.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文