When initialising a Leaflet map with Backbone.js I can't access this exact map anymore.
E.g.:
mapView.map.locate({setView: true, maxZoom: 10});
will result in
TypeError: 'undefined' is not a function (evaluating 'mapView.map.locate({setView: true, maxZoom: 10})')
The map is initialized via a separate view in a dashboard view like:
this.mapView = new MapView();
$(this.$el).find('.content').append(this.mapView.el).find('.map').addClass('full').css({
'height': $(window).height() / 2
});
This view looks like:
var MapView = Backbone.View.extend({
template: _.template(""),
render: function ()
{
this.$el.html(this.template());
this.map = L.map(this.el).setView([48.00786, 13.17989], 8);
this.map = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'OpenStreetMap © Mitwirkende der <a href="http://osm.org/copyright">OpenStreetMap</a>'
}).addTo(this.map);
return this;
}
});
I can access the map object by just console.log'ing it. The result looks like:
_animated: true _bgBuffer: HTMLDivElement _clearBgBufferTimer: 4 _container: HTMLDivElement _initHooksCalled: true _leaflet_events: Object _leaflet_id: 20 _limitedUpdate: function s() {var a=arguments;return n?(o=!0,void 0):(n=!0,setTimeout(function(){n=!1,o&&(s.apply(i,a),o=!1)},e),t.apply(i,a),void 0);} _map: Object _tileContainer: HTMLDivElement _tileImg: HTMLImageElement _tiles: Object _tilesToLoad: 0 _url: "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" options: Object proto: Object
But why am I not able to access this map afterwards?
Thanks!
mapView.map.locatewould suggest that you're expecting to find amapViewvariable when you only have amapViewproperty in one of your views. And don't$(this.$el), just usethis.$el;this.$is the same asthis.$el.find. - mu is too short