Javascript 在主干视图backbone.js中从另一个函数调用一个函数
我想要的是使用这个.plotPort()从plotLoc调用plotPort;但是我不能。。。但是如果我使用self.plotPort();它对IE不起作用。 我的解决方法是在重置调用plotPort时向lLoca添加事件。 为什么这个.plotPort不起作用Javascript 在主干视图backbone.js中从另一个函数调用一个函数,javascript,jquery,html,backbone.js,Javascript,Jquery,Html,Backbone.js,我想要的是使用这个.plotPort()从plotLoc调用plotPort;但是我不能。。。但是如果我使用self.plotPort();它对IE不起作用。 我的解决方法是在重置调用plotPort时向lLoca添加事件。 为什么这个.plotPort不起作用 var Loca = Backbone.Model.extend({latitude:{},longitude:{}}); var LocaList = Backbone.Collection.extend({
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;
我从您的代码中了解到,在执行
fetch
之前,您应该绑定reset
,而且绑定不正确,应该是这样的
lLoca.on('reset', this.plotPort, this); // notice () being removed after this.plotPort
lLoca.fetch(lOptions, this);
这将使用方法绑定重置
,而不是调用它
关于从一个方法调用另一个方法,这很简单,我们只是使用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.
}
});
这只是一个例子,但同样的概念也可以用于类似的问题。我建议在初始化函数的开头使用
.bindAll(This)
。在初始化函数中尝试一下
_.bindAll(this,'plotPort', 'plotLoc');
var lOptions = {};
lOptions.success = this.plotLoc;
lLoca.fetch(lOptions,this);
lLoca.on('reset', this.plotPort, this);
在对象内部,从一个函数调用另一个函数,可以使用jQuery代理传递正确的“this” 而不是
this.plotPort();
试一试
这是一个简单的引用变量,应该可以工作,试着给它起一些不同的名字,比如
var$this=this
并使用$this
。可能是原因。谢谢,你知道一个好的教程可以帮助开发记住IE 8Welcome:)没有认知欲望,不幸的是,我不知道任何类似的教程:(
$.proxy(function () {
this.plotPort();
}, this);