--
You received this message because you are subscribed to the Google Groups "AngularJS" group.
To post to this group, send email to ang...@googlegroups.com.
To unsubscribe from this group, send email to angular+u...@googlegroups.com.
Visit this group at http://groups.google.com/group/angular?hl=en-US.
Hi!I see a couple ways to do this but, as Peter said, using the DOM is not a good idea. Here are two solutions to think about.You could use events. If your directive listens for events (`$scope.$on`) then your controller can $broadcast events (with optional arguments) to your directive. This has tradeoffs and won't be good in every case. But you could potentially "expose" an API through event listeners.But I imagine in many cases this will be unnecessary and overly-complicated. In your video player example, some methods normally exposed as an API work really well as attributes. You don't need something like `directive.play(file)` because you can have a `file` attribute, and the directive can respond to changes in it. You could also have state, timecodes, etc., as attributes and, with bi-directional binding, you get a much more seamless interface between controller and directive. Plus, you need to know neither anything about the internals of the directive nor anything about its DOM structure. For many directive use cases, attributes _are_ the exposed API.Anyone have other thoughts?Josh
The solution seems to be ok, but the final judgement would be possible after looking at implementation.
All I wanted to say is that you have to place the model-controller always in the privileged position.
Question: do you write unit tests? If not, do you write your code as if you were writing them? In either case you would never let the directives drive your user stories.
Thinking this way: let you write,in list, all your expectations about how would you like user to use your application. Could you codify all that as the expectations and mocks against controllers, services, events and whatever else there is, excluding view artifacts?
Of course without being paranoic. For example: you have simple paginator. It knows how many items there is, which page you are on and what is the number of items per page. There is no need to expose trivial methods on scope like
isFirstButtonEnabled()
isNextButtonEnabled()
etc...
Why? Because that methods are so trivial you would't like to waste your time writing unit tests for them. You can go ahead and explicitly implement them on the view:
<a href... ng-enabled="page>1">...</a>
But once it is getting tricky like when other parties do participate in the situation, you would certainly like to hide/encapsulate the logic in controller and use all the greatness of AngularJS to decouple that logic from DOM/view stuff, staying st the simple level of pure JavaScript (the language), so you can test-drive the use cases in isolated environment. Your view will stay simple no matter what.
Regards,
Witold Szczerba
---
Sent from my mobile phone.