dev-resources.site
for different kinds of informations.
Add custom layer to embe-leaflet
Published at
9/4/2024
Categories
ember
javascript
leafletjs
Author
michalbryxi
Author
11 person written this
michalbryxi
open
The problem
ember-leaflet is a very popular addon from EmberJS ecosystem that allows a lot of flexibility.
But what if I want to extend it's functionality so that it can do even more? And what if I want it as a new yielded component directly from the <LeafletMap>
s layers
?
The solution
At first we will need our new component. For simplicity this component will just extend an existing layer component from the addon. Let's use the marker
component and make it so that it just ignores location argument and sets a fake, hardcoded value:
// app/components/fake-marker-layer.gts
import MarkerLayer from 'ember-leaflet/components/marker-layer';
export default class FakeMarkerLayer extends MarkerLayer {
get location() {
return this.L.latLng(46.68, 7.85);
}
}
After this we will need to register the component with ember-leaflet
service:
// app/instance-initializers/leaflet.ts
import FakeMarkerLayer from '../components/fake-marker-layer';
import type Owner from '@ember/owner';
export function initialize(owner: Owner) {
const emberLeafletService = owner.lookup('service:ember-leaflet');
if (emberLeafletService) {
emberLeafletService.registerComponent('fake-marker-layer', {
as: 'fake-marker',
component: FakeMarkerLayer,
});
}
}
export default {
initialize,
};
And now we can use it:
import LeafletMap from 'ember-leaflet/components/leaflet-map';
<template>
<LeafletMap
@lat={{46.6}}
@lng={{7.8}}
@zoom={{15}}
as |layers|
>
<layers.fake-marker @lat={{999.99}} @lng={{0}} />
</LeafletMap>
</template>
Notes
You can read on this technique also on the official ember-leaflet documentation page.
ember Article's
30 articles in total
Ember.js in 60 Seconds
read article
Intro to Ember.js Components
read article
The Top 7 Reasons You Should NOT Use Ember.js on Your Next Project
read article
why don't we have a slack channel?
read article
Installing EmberJS v2 addons from GitHub forks using PNPM
read article
Add custom layer to embe-leaflet
currently reading
Why Ember Wins My Heart Over React β€οΈ And Maybe Yours Too!
read article
Migrate from ember-cli-deploy-sentry to sentry-cli
read article
ERR_PNPM_BAD_PM_VERSION This project is configured to use vX of pnpm. Your current pnpm is vY
read article
Fixing Package Dependencies
read article
React inside Ember - The Second Chapter
read article
π Rendering Dynamic Components in Ember.js with Embroider
read article
How to use the new Ember theme for QUnit
read article
Effects in Ember
read article
unsupported ambiguity between helper and component
read article
12 common Ember errors to know: A definitive guide on handling EmberJS errors
read article
Demystifying Ember.js Development: How to Hire the Right Developer for YourΒ Project
read article
Is Angular.js or Ember.js the better choice for JavaScript frameworks?
read article
Integrations Series: Authentication And Connection
read article
Support for in/inter page linking / scrolling in EmberJS
read article
Ember-cli config
read article
GraphQL in Your Ember App with Glimmer Apollo
read article
Setting up Tailwind, the easy way
read article
Context leaking in EmberJS tests
read article
What to use instead of `@ember/string`
read article
Ember Language Server in Emacs
read article
Why Ember.js is designed for Enterprise Software Development
read article
Demystifying Ember Serialization: A Comprehensive Guide
read article
ember-cli is stuck on certain version
read article
React micro-frontend in ember app - Part 2
read article
Featured ones: