0

Google ストリート ビューをレンダリングする次のバックボーン ビューで問題が発生しています。

問題は、processSVData 関数でthisが のインスタンスではないことですApp.DetailStreetView。中にconsole.log(this)入るprocessSVData()と、DOMWindowオブジェクトを取得します。したがって、アクセスしようとするthis.panoramaと、undefined

App.DetailStreetView = Backbone.View.extend({
    initialize: function() {
        this.latLng = new google.maps.LatLng(37.869085,-122.254775);
        this.panorama = new google.maps.StreetViewPanorama(this.el);
    },
    render: function() {
        var sv = new google.maps.StreetViewService();
        sv.getPanoramaByLocation(this.latLng, 50, this.processSVData);        
    },
    processSVData: function(data, status) {
        if (status == google.maps.StreetViewStatus.OK) {
            // calculate correct heading for POV
            //var heading = google.maps.geometry.spherical.computeHeading(data.location.latLng, this.latLng);
            this.panorama.setPano(data.location.pano);
            this.panorama.setPov({
                heading: 270,
                pitch:0,
                zoom:1, 
            });
            this.panorama.setVisible(true);
        }
    },
});
4

1 に答える 1

1

いくつかのオプションがあります。適切なもの_.bindAllにバインドするために使用できます:processSVDatathis

initialize: function() {
    _.bindAll(this, 'processSVData');
    //...
}

これにより、this常に内部のビューが作成されprocessSVDataます。

_.bindコールバックだけに使用することもできます。

sv.getPanoramaByLocation(this.latLng, 50, _.bind(this.processSVData, this));

これにより、がコールバックとして呼び出されたthisときのビューが保証されます。または(ブラウザのバージョンの問題について心配する必要がない場合)でも同様のことができます。this.processSVDatasv.getPanoramzByLocation$.proxyFunction.bind

または、通常のjQueryスタイルで手動で行うこともできます。

var _this = this;
sv.getPanoramaByLocation(this.latLng, 50, function(data, status) {
    _this.processSVData(data, status);
});

最初の、_.bindAllは、おそらくバックボーンで最も一般的なアプローチです。

于 2012-06-16T02:47:35.347 に答える