Hi Sander,

thanks for your answer.

Yes I have lots of directives with a controller. At the moment they receive 
the initial data set via a parameter and manipulate it in their controller 
if needed. The issue I was mentioning with nr. 1 to 10 and 11 to 20 was 
connected to directives calling for data on the service and the service 
saving the data directly in its object. Lets say you have thousands of 
complex items then your service can't hold every item but has to fetch them 
on demand.

So I still don't really know if its better to provide a directive with data 
via a parameter or let them fetch the data itself via a service. I wanted 
to try the second version because my directives already have the dependency 
to the service. But even if I separate state and data the data needs to be 
synchronized between my directives.

To clarify I added a simple minified code example:

.directive('firstDirective', function (myService){
    return {
        controller: function ($scope) {
            myService.getData().then(function (data) {
                $scope.myData = data;
            });
        }
    }
})
.directive('secondDirective', function (myService){
    return {
        controller: function ($scope) {
            myService.getData().then(function (data) {
                $scope.myData = data;
            });
        }
    }
});

Lets say myService.getData() fetches data from the server or from cache if 
enabled. Because the $http cache does not return a reference to the same 
object both directives have initially the same data but the data is not 
synchronized if one component changes it. I can't just simply save the data 
object in the service because of possible different requests like 
mentioned. I think what I would need is something like a cache that returns 
for the same request an

On Wednesday, December 3, 2014 6:03:44 PM UTC+1, Sander Elias wrote:
>
> Hi André.
>
> You know directives can have controllers right? (just a slight 
> side-remark, not much to do with your issue!)
>
> You should separate your state, from your data. the state (I need nr 1 to 
> 10, and I need it ordered by zzyy..) needs to be in your directive.
> The data, should be in a shared service. 
> This allows you to do whatever you want. If you have some really crazy 
> things in your directive, you can even create a copy of the data in memory, 
> and write it back whenever needed.
> There will be a tax involved in that (added complexity and memory 
> requirements), but it is a good way to keep the separations in place.
> If it's something you need to do often, you might even create a service 
> that does the copying and re-integration of the data.
>
> Regards
> Sander
>

-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to