简体   繁体   English

在骨干视图Backbone.js中调用另一个函数

[英]calling one function from another in backbone view Backbone.js

What I want is to call plotPort from plotLoc using this.plotPort(); 我想要的是使用this.plotPort()从plotLoc调用plotPort; but I am unable to... but if I use self.plotPort(); 但我无法......但如果我使用self.plotPort(); it does not work for IE. 它不适用于IE。 My workaround was to add event to lLoca upon reset to call plotPort. 我的解决方法是在重置时向lLoca添加事件以调用plotPort。 Why does not this.plotPort work? 为什么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;

What I can understand from your code, you should bind reset before doing fetch and also the binding is not proper it should be like this, 我可以从你的代码中理解,你应该在执行fetch之前绑定reset ,并且绑定不正确它应该像这样,

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

This will bind the reset with the method instead of calling it. 这将使用方法绑定reset而不是调用它。

And about calling one method from another its pretty straight forward, we just call it using this , but if that call is being made from some callback function or any other similar thing where this does not refer to the view, then its advised to save the reference of this before that callback and use it whenever required in the callback. 而关于从另一个调用一个方法,它非常简单的,我们只是把它用this ,但如果呼叫正从一些回调函数或其他任何类似的事情在那里做this并不是指来看,那么它的劝救参考this是回调之前,并使用它时,需要回调。 For example, 例如,

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

This is just an example, but same concept can be used for similar problems. 这只是一个例子,但同样的概念可以用于类似的问题。

我倾向于在初始化函数的开头使用_.bindAll(this)

try this in your initialize function 在初始化函数中尝试这个

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

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

inside an object , from one function calling another function, you could use jQuery proxy to pass in the correct "this" 在一个对象内部,从一个函数调用另一个函数,你可以使用jQuery代理传入正确的“this”

instead of 代替

    this.plotPort();

try 尝试

    $.proxy(function () { 
        this.plotPort(); 
    }, this);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM