This is really a rather generic question, but as people on IRC appeared to have issues understanding what I was trying to do, I've provided some rather specific information in this post. The answer would likely be applicable to a far wider range of issues than just this specific one.
I'm looking to have one directive inherit behaviour of another directive, while also providing behaviour of its own. Below is an example case.
Say I have the following document:
<body ng-app="AppCore" dock-container>
<pane dock="left" class="one">
left
</pane>
<pane dock="fill" class="two" title="middle">
<pane dock="top" class="four" height="100">
middle top
</pane>
<pane dock="fill" class="five">
middle fill
</pane>
</pane>
<pane dock="right" class="three" title="whatever">
right
</pane>
</body>
Here, I have three different directives:
pane: An element directive. This represents a UI element (a pane or 'panel', as the name implies).dock-container: An attribute directive. This acts as a modifier; it adds some bookkeeping data to the scope, and marks it as being a 'container'.dock: An attribute directive. This also acts as a modifier; it indicates that the element should be positioned relative to the nearestdock-containerparent - 'docking' here refers to making the element 'stick' to a side of the nearest container (or fill up the remaining space).
Now, the problem is that a pane should also implicitly be a dock-container - that is, it should behave exactly the same as an element with the dock-container directive would, in addition to pane-specific behaviour.
However, in the interest of clean markup, I don't want to have to explicitly specify dock-container on every pane - it should implicitly inherit from dock-container without having to actually specify the dock-container directive.
In other words, I'm looking to make my pane directive behave as if it were like this:
<pane dock-container>
... while it actually says this in the document:
<pane>
I don't particularly care how the pane directive inherits its behaviour from the dock-container directive, even if it means having the pane directive add the dock-container directive itself before Angular finishes processing the element. Ideally, the link function of the dock-container directive, not just the controller, would also be applied to the pane.
I am currently using element.parent().controller('dockContainer') to find the nearest dock-container from a dock directive - for this to work, the controller that I've defined for the dock-controller directive would have to be present on the pane element, regardless of the method of inheritance.
If your suggested inheritance implementation requires a different method of finding the nearest dock-container (or pane), that's fine too - the problem here is just the inheritance, the rest can be changed to accommodate the solution to this problem.
The following are not suitable options:
- Copy-pasting the code for
dock-containerto thepanedirective. I want to keep code duplication to a minimum, and it's quite possible that other directives in the future should also inherit fromdock-container. - Manually adding
dock-containerto everypaneelement - this is exactly what I'm trying to avoid. The code I'm working on is meant to be used as a reusable set of directives, and I aim to keep the syntax as clean as possible. Everypaneshould inherit thedock-containerbehaviour, so there's no added value in requiring it to be specified explicitly. - Anything that overrides the behaviour for the
panedirective. Thedock-containerbehaviour should be an addition, not a replacement. Thepanedirective will also have some specific behaviour of its own, that thedock-containerdirective itself does not have. - Anything that makes it impossible for an element to have both a
dockanddock-containerdirective - the search for the 'nearestdock-container' starts at the parent of the element, so an element will never be its owndock-container. - Anything that changes the type of element. The
paneelement should stay apaneelement.