将事件放在模型上还是控制器上?
这是针对使用被动视图的客户端 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为这取决于想要知道联系人何时保存到数据库与想要知道用户何时提交保存联系人页面之间微妙但重要的区别。即,控制器可能不是保存联系人的唯一地方。如果您想了解前者,请使用模型。如果您想了解后者,请使用控制器。
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.
我会推荐以下内容。
该命令执行以下操作
然后在处理查询的 Presenter 中,更新联系人调用将查找所有相关查询并更新联系信息。此更新可以通过侦听器模式来完成,其中所有查询视图都向呈现器注册自己。
*正确的演示者是您设置来处理显示查询的视图的任何演示者。某些系统可能具有用于显示查询的唯一演示器,而在其他系统中,它可能只是演示器的一部分,其中查询显示只是较大显示器的一部分。
I would recommend the following.
The Command does the following
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.