jmls
jmls

Reputation: 2969

angular services /models data and controller : how to share the data

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

Upvotes: 2

Views: 76

Answers (3)

ssayyed
ssayyed

Reputation: 796

Think of it this way, if you know when you want to read from the service, then there is no need to use $broadcast. At the end of the day that gives you more control over what is happening, as broadcasting would propagate event across your scope it could be more error prone.

If you want your scope and any child scopes for example to to do something upon an event, then it would be a good way to put $broadcast in use.

In your example, if data is not used anywhere else in the application. then you can keep it in the controller. The service would help you share data with other directives or controllers. Also it allows you to have business logic to reject/resolve response from server, which will be separate from controller.

Your controller

var data = service.getData(); 

find(id); save(id);

Your service

 getData: {
      return // data from db;
  }

Upvotes: 0

Bharat Bhushan
Bharat Bhushan

Reputation: 2097

For notifying others instance of your factory or service you have to fire events or use $watch feature of angular.

Here is example

Upvotes: 1

manasi sakhare
manasi sakhare

Reputation: 1051

According to what I have understood of the Controller-Service model, the service is not event driven. By Event, I refer to a user action that initiates an event (e.g a mouse-click). This kind of event driven behavior is of the Controller of a view. But in Services, one should ideally communicate to the server (backend) to either read, modify or delete data. Or it may contain web services called for any special purposes other that the normal CRUD. The data which it holds is a result of the services response. Service is a facade between the controller of the view (on client/browser) and the back-end(web-service which communicates to the client).

The event publishing and subscribing is either from directive to controller, or controller to another controller etc.

Upvotes: 0

Related Questions