So I've got the point where I have a service and a controller. The service reads/writes data from the datasource and the controller is aware of this data. All is peachy.
Except for a nagging thought in my mind. What is the best way for the service to notify the controller that there is modified data ?
At the moment, I have (simple pseudo-code) this in the service:
var data = {
items: [],
item: {}
}
// read & write functions to the dataSource : ***data.items*** gets populated with the list of records from the db
function find(id) {
//find appropriate item, ***data.item*** gets populated
// with the record
function save() {
// write data.item to the db
}
in the controller we have
{ // service is injected as a dependency
controller.service = service
controller.data = controller.service.data
controller.save = function() {
controller.service.save()
}
// etc etc
this all works fine. I must stress that all my ui binding works, and works well.
However,
a) I feel uneasy with the state being held by the service b) I'm not sure that getting the controller to point to the data held by the service is the best way of linking the two together.
a) Would it be better for the controller to pass the object to modify as a parameter to the save function
controller.save = function() {
service.save(controller.data.item)
b) instead of directly accessing the service data, would it be better for the service to broadcast a message containing the data and the controller to subscribe to this message ?
I suppose I'm boiling the question down to this :
is it better for the service to send & receive data, or for the controller to have direct access to the service data ?
thanks