Search code examples
model-view-controllermvp

Put an event on the model, or on the controller?


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!


Solution

  • 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.