2012-11-02 32 views
0

我想要的是使用this.plotPort()從plotLoc調用plotPort;但我無法......但如果我使用self.plotPort();它不適用於IE。 我的解決方法是在重置時向lLoca添加事件以調用plotPort。 爲什麼this.plotPort不起作用?在骨幹視圖中調用另一個函數Backbone.js

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; 

回答

2

我可以從代碼中瞭解什麼,你應該做fetch之前綁定reset,也是結合是不妥當的,應該是這樣的,

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. 
    } 
}); 

這只是一個例子,但是對於類似的問題可以使用相同的概念。

+0

自我不工作的情況下IE 8 – CognitiveDesire

+0

它的一個簡單的引用變量,它應該工作,試着給它一些不同的名稱,如'var $ this = this'並使用'$ this'。 可能[this](http://stackoverflow.com/questions/2970263/jquery-javascript-and-ie8)是原因。 – Cyclone

+0

謝謝,你知道一個很好的教程,可以幫助在開發中記住IE 8 – CognitiveDesire

0

我會在初始化函數開始時使用_.bindAll(this)

+0

它不起作用:( – CognitiveDesire

0

試試這個在您的初始化函數

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

     var lOptions = {};     
     lOptions.success = this.plotLoc; 
     lLoca.fetch(lOptions,this); 
     lLoca.on('reset', this.plotPort, this); 
+0

嘗試但不工作 – CognitiveDesire

0

對象內,從一個函數調用另一個函數,你可以使用jQuery的代理在正確的通過「本」

,而不是

this.plotPort(); 

嘗試

$.proxy(function() { 
     this.plotPort(); 
    }, this); 
相關問題