0

私が欲しいのは、this.plotPort(); を使用して plotLoc から plotPort を呼び出すことです。しかし、できません...しかし、self.plotPort();を使用すると IE では機能しません。私の回避策は、リセット時に lLoca にイベントを追加して、plotPort を呼び出すことでした。this.plotPort が機能しないのはなぜですか?

var Loca = Backbone.Model.extend({latitude:{},longitude:{}});
        var LocaList = Backbone.Collection.extend({
            model : Loca,
            url : "/getLatLongList"
        });
        var PortList = Backbone.Collection.extend({
            model : Loca,
            url : "/getLatLongListForPort"
        });
        var lLoca = new LocaList;
        var lPort = new PortList;

        var CreateMap = Backbone.View.extend({

                    el : 'div',
                    map : {},
                    latitude : {},
                    longitude : {},             
                    initialize : function() {

                         var lOptions = {};                  
                    lOptions.success = this.plotLoc;
                        lLoca.fetch(lOptions,this);
                        lLoca.on('reset',this.plotPort(),this);
                        //_.bindAll(this,'plotPort');
                    },

                    plotLoc : function() {
                        // var test =lLoca.toJSON();
                        // $('#pchart').html(JSON.stringify(lLoca));

                        this.latitude = lLoca.toJSON()[0].latitude;
                        this.longitude = lLoca.toJSON()[0].longitude;
                        this.latlng = new google.maps.LatLng(this.latitude,
                                this.longitude);
                        var mapOptions = {
                            center : this.latlng,
                            zoom : 15,
                            mapTypeId : google.maps.MapTypeId.SATELLITE,
                            mapTypeControl : false,
                            streetViewControl : false,
                            navigationControlOptions : {
                                style : google.maps.NavigationControlStyle.SMALL
                            }
                        };
                        mapContainer = $("#mapContainer").get(0);
                        this.map = new google.maps.Map(mapContainer, mapOptions);

                        _.each(lLoca.models, function(loc) {
                            var marker = new google.maps.Marker({
                                position : new google.maps.LatLng(
                                        loc.toJSON().latitude,
                                        loc.toJSON().longitude),
                                map : this.map,
                                title : ""
                            });

                        }, this);
                        lLoca.reset();
                        //console.log(self+"");
                    //this.plotPort();
                    },
                    plotPort : function() {

                         lPort.fetch({                  
                         success: function() {
                             var postImage = new google.maps.MarkerImage(
                                        "Images/greenflag.png",
                                        new google.maps.Size(50, 50));
                                var marker = new google.maps.Marker({
                                    position : new google.maps.LatLng(
                                            lPort.toJSON()[0].latitude,
                                            lPort.toJSON()[0].longitude),
                                    map : this.map,
                                    icon : postImage,
                                    title : "From"

                                });
                                marker = new google.maps.Marker({
                                    position : new google.maps.LatLng(
                                            lPort.toJSON()[1].latitude,
                                            lPort.toJSON()[1].longitude),
                                    map : this.map,
                                    icon : postImage,
                                    title : "To"

                                });
                         }
                         });                        
                    },
                    render : function() {
                        return this;
                    }

                });
        var App = new CreateMap;
4

4 に答える 4

2

あなたのコードから私が理解できることは、実行するreset前にバインドする必要fetchがあり、バインドが適切ではなく、次のようになるはずです。

lLoca.on('reset', this.plotPort, this); // notice () being removed after this.plotPort
lLoca.fetch(lOptions, this);

resetこれは、メソッドを呼び出す代わりにメソッドにバインドします。

そして、あるメソッドを別のメソッドから呼び出すことについては非常に簡単ですが、 を使用して呼び出すだけthisですが、その呼び出しがビューを参照しないコールバック関数または他の同様のものから行われている場合はthis、 の参照を保存することをお勧めしますthisそのコールバックの前に、コールバックで必要なときにいつでも使用します。例えば、

var _view = this;
// doing collection fetch

this.collection.fetch({
  success :  function (collection, response) {
    // if you want to access view here, it can be accessed using 
    // '_view' variable, because 'this' here does not point to the view.
  }
});

これは一例にすぎませんが、同様の問題にも同じ概念を使用できます。

于 2012-11-02T13:13:36.363 に答える
0

object 内で、ある関数が別の関数を呼び出している場合、jQuery プロキシを使用して正しい「this」を渡すことができます

それ以外の

    this.plotPort();

試す

    $.proxy(function () { 
        this.plotPort(); 
    }, this);
于 2012-11-07T09:28:50.587 に答える
0

_.bindAll(this)初期化関数の最初に使用することをお勧めします。

于 2012-11-02T14:45:46.007 に答える
0

初期化関数でこれを試してください

      _.bindAll(this,'plotPort', 'plotLoc');

      var lOptions = {};                  
      lOptions.success = this.plotLoc;
      lLoca.fetch(lOptions,this);
      lLoca.on('reset', this.plotPort, this);
于 2012-11-02T15:07:03.433 に答える