-
Notifications
You must be signed in to change notification settings - Fork 655
Extending Components
A MapComponent
inherits from Vue
, and additionally supports the following:
-
DeferredReady
: providesdeferredReady
hook
-
deferredReady: Returns
Promise | value
. In aMapComponent
,deferredReady
is not called untilvm.$map
has been set to thegoogle.maps.Map
instance.
-
$map: The
google.maps.Map
object of its nearestMap
ancestor. This is only guaranteed to be available atdeferredReady
, not atready
.
-
deferredReady: Returns
Promise | value
. Called after ancestors'deferredReady
has been called, and if they return Promises the Promises have been resolved. If a Promise is returned, its descendents'deferredReady
will not be called until the Promise is resolved.
It is easy to write new Google Maps components! (or components that depend on Google Maps). An example is shown in examples/overlay.html
.
Simple put, create a new component by extending the MapComponent
provided for you:
import {MapComponent} from 'vue-google-maps'
Vue.component('ground-overlay', MapComponent.extend({
deferredReady: function() {
}
}));
If you need to refer to the google.maps.Map
object, use vm.$map
, e.g.:
deferredReady: function() {
this.$myComponent = new google.maps.Some.NewComponent({
...
map: this.$map
})
}
In Vue, if you have component A
, which has child component B
, which has child component C
, the ready
hooks are called in the following order:
C::ready --> B::ready --> A::ready
Unfortunately, the Google Maps API can only be initialized at or after A::ready
. To fix this, we have added a new hook deferredReady
that fires either (1) after the promises returned by its ancestors' deferredReady
have resolved, or (2) after the component's ready
has completed, if none of its ancestors support deferredReady
.
deferredReady
support can be mixed into any component through the DeferredReadyMixin
:
import {DeferredReadyMixin} from 'vue-google-maps'
Vue.component('some-component', { mixins: [DeferredReadyMixin] })
deferredReady
allows a component to ensure that its ancestor's asynchronously loaded dependencies have been loaded.
To use the example of clustered markers:
Map::ready
--> Map::deferredReady (returns Promise that is resolved when Google Maps is loaded)
--> (idle)
--> *maps loaded*
--> MarkerClusterer::deferredReady : new MarkerClusterer({... map: this.$map ...})
--> Marker::deferredReady : $clusterObject.addMarker( new google.maps.Marker({ ... map: this.$map ... }) )
- Map
- [MapComponent](Extending Components)
- Marker
- Cluster
- InfoWindow
- Polyline
- Circle
- Rectangle
- PlaceInput
... More to come
(for contributors)