One thing about app organization I haven't quite nailed down in my head is 
whether or not an app's features should be split up into a bunch of 
directives or whether they should be controllers that use smaller more 
generic directives to compose functionality. I've seen it done both ways. 
I've seen apps where a huge feature is wrapped inside a directive with a 
template and a tiny controller basically just works with ng-view and 
angular's router to serve a small template with a single element that 
invokes the much larger directive. I've also seen apps where most of the 
business logic is in controllers and view templates and directives are 
extremely generic and relatively small and simple. 

I wanted to get a sense of what people are doing these days. So far, the 
rule of thumb I've come up with is that directives should really only ever 
be used if you plan to re-use the functionality they implement extensively. 
Otherwise just stick to controllers and view templates/partials. Also, keep 
any logic dealing with server communication or data manipulation/gathering 
out of directives. Data should be properly manipulated and passed into a 
directive from a controller which in turn should be relying on services to 
do the bulk of that work. That way your directives don't rely on specific 
requirements related to data retrieval. 

To make this post clearer, here's an example of a scenario I am struggling 
with:

Let's say you have blog app. You have an index page that has blog posts. 
The blog index page's logic should go in a controller which uses a service 
to retrieve the blog posts and attach them to the scope. I don't think a 
directive that wraps the index of blog posts would make sense here. 
However, the blog posts themselves may have a lot of logic tied to them. 
That logic shouldn't go in the blog post index page. But should one create 
a "blogPost" directive in this case and throw all the logic related to 
individual blog posts in there? Or should it just be an ng-controller tag 
on each blog post? What if this is the only place you'll ever use that 
blogPost directive? What's the value in even making it a directive? The 
ability to isolate the scope? 

In short, how much should directives know about your apps business logic? 
Should they be ultra generic like the built in ng-* directives?

-- 
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/groups/opt_out.

Reply via email to