私はこの男を理解しようと想像できる限り多くのStackOverflow/googleグループを経験してきました。
BackboneJSを使用して、開始位置と終了位置を持つマップをレンダリングしています。新しいページ/ページの更新では、このエラーは発生せず、jQueryの$(window).load(.....)関数を使用しているため、マップなどは正常に機能します。ただし、ビューを動的にレンダリングすると、DOMがまだDIVをロードしていないため(document.getElementByIdで失敗)、このエラーが発生します(私は信じています)。$(window).load()以外のさまざまなメソッドを試しましたが、両方のユースケース(新しいページの読み込み-BackboneJSビューの読み込み)で機能するものを取得できません。テンプレートの直後に関数を呼び出そうとしても機能しません。
どんな助けでもいただければ幸いです。
ロバート
意見:
App.Views.MapShow = Backbone.View.extend({
initialize: function() {
_.bindAll(this, 'render');
var self = this;
$(window).load(function() {
self.renderMap();
});
},
render: function() {
this.renderTemplate();
},
renderTemplate: function() {
this.$el.html(JST['path/to/show/file']());
},
renderMap: function() {
var from = this.model.get('location_from');
var to = this.model.get('location_to');
var geocoder = new google.maps.Geocoder();
var map = new google.maps.Map(document.getElementById('mapCanvas'), {
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
directionsDisplay.setMap(map);
var request = {
origin: from,
destination: to,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
});
HTML:
<div class="map" id="mapCanvas"></div>