question about directives #29
Description
By chance I wound up subscribed to this repo, so I finally clicked on the GitHub mails to see what it was.
Seems like an idea that should replace just about every "AngularJS seed" project out there. Makes me wish I could just npm unpublish generator-angular-* --force
. Anyway, question:
A lot of people will create what I refer to as 'one-off' directives. They should usually just be sub-states. If you create directives that are specific to your app's business logic, and aren't focused on purely UI visual implementation (regardless of data, application, etc) then you are too tightly coupling your business logic to your view. You are making it more difficult to quickly refactor your view or view structure. You have to track down where business logic is being executed or modified in multiple places. > You start keeping track of data state and lifecycle and implementing things like events and streams because your view lifecycle isn't consistent with your data lifecycle.
Instead, 0 business logic in views. Rendering logic in views only. Publicly, reusable, agnostic, unopinionated, highly versatile/reusable view logic.
If you're keeping all of your business logic where it should be, what is the problem with one-off directives? I tend to use them simply to get isolated scopes (and deter developers from using the scope hierarchy) and completely eschew .controller()
controllers altogether.
If you're buying what I'm selling here, perhaps changing this line:
If you can't open-source your directives, they probably shouldn't exist
To something like:
Keep your godawful business logic out of directives too
Granted, many directives I write do get open-sourced, but others are app-specific, biz-logic-less one-offs which just control a handful of widgets. Technically reusable, but nobody would want them.