Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abstract away leaflet directive in custom directive

I'm currently working on an Angular 6 application that uses ngx-leaflet for using leaflet. We want to be able to create a base component that can be customized by adding directives to it. Basically the same pattern that is used when using draw capabilities using leafletDraw. But these directives should be more abstract then the leaflet and leafletDraw directives.

Currently we ended up with the following template that is used in our "base component"

<div class="map" leaflet [leafletOptions]="options" [leafletLayers]="layers" [leafletOptions]="options" leafletDraw [leafletDrawOptions]="drawOptions" poiSelection [poiSelectionOptions]="selectionOptions"
poiSelection [trackPlaybackOptions]="trackOptions">
</div>

As you can see this can end up in a big wall of directives on different abstraction levels.

I rather have this :

<app-our-nice-map poiSelection [poiSelectionOptions]="selectionOptions" [trackPlaybackOptions]="trackOptions">
</app-our-nice-map>

And the template for the OurNiceMapComponent component would look like this :

<div class="map" leaflet [leafletOptions]="options" [leafletLayers]="layers" [leafletOptions]="options">
</div><!-- this is here because Stackoverflow doesn't like single div /-->

There's two challenges with this.

  1. Finding a way on how a higher level directive can be interacting/configuring (poiSelection) with a lower level directive (leafletDraw).
  2. How to add a directive dynamically. As you can see in the above template for OurNiceMapComponent I didn't add leafletDraw, because that is only used by a specific higher level directive.

So the question is, how to create these "higher order directives".

like image 937
Saab Avatar asked Jul 31 '18 08:07

Saab


People also ask

What are attribute directives and how to use them?

When no additional template is required, attribute directives, also known as custom directives, are utilised. The directive has the ability to run logic and make visual modifications to the element to which it is applied. If you want to change the behaviour or style of existing HTML elements without creating a new component, this is a good option.

What is the difference between a custom Directive and component?

Custom directives, on the other hand, are mainly intended for reusing logic that involves low-level DOM access on plain elements. A custom directive is defined as an object containing lifecycle hooks similar to those of a component. The hooks receive the element the directive is bound to.

What is a custom directive in Vue?

In Vue, what is a custom directive? Directives are special attributes that begin with the letter v. When the value of a directive's expression changes, the directive's role is to reactively apply side effects to the DOM. When would a custom directive be useful?

What is the difference between heading 2 and V-Magic directives?

On the other hand, the second Heading 2 is styled using a custom directive called v-magic. The components accept a directives option where you can define your custom directives. As you can see, the purpose of both the directives is the same, but the syntax is quite different.


1 Answers

There is workaround, you can use ngSwitch/ngIf and render leaflet with correct set of directives depending on component options.

like image 113
kemsky Avatar answered Oct 05 '22 01:10

kemsky