我尝试删除包含在地图中呈现的制作者的视图。当我单击按钮时,事件click .ver
正在激活,但没有任何反应,我收到以下错误:Uncaught TypeError: undefined is not a function
。我不知道我做错了什么。
我的代码:
ev.views.Home_Eventos = Backbone.View.extend({
initialize: function(map){
this.template = _.template(ev.templateLoader.get('home_list_eventos'));
this.map = map;
//console.log(this.map);
this.render(this.map);
},
events: {
'click .ver' : 'teste'
},
teste: function(){
ev.views.Markers.remove();
},
render: function(map){
var that = this;
that.map = map;
var imagens = new ev.models.ImagemCollection();
imagens.fetch({
success: function(){
that.$el.html(that.template({imagens: imagens.models}));
var marcadores = imagens.models;
setTimeout(function() {
that.local(that.map);
var marcadores = new ev.views.Markers(that.map);
}, 0);
return that;
}
});
}
});
ev.views.Markers = Backbone.View.extend({
initialize: function(map){
this.map = map;
this.render(this.map);
},
render: function(map){
var that = this;
var imagens = new ev.models.ImagemCollection();
imagens.fetch({
success: function(){
var marcadores = imagens.models;
setTimeout(function() {
_.each(marcadores, function(marcador){
var myLatlng = new google.maps.LatLng(marcador.get('latitude'),marcador.get('longitude'));
var marker = new google.maps.Marker({
position: myLatlng,
map: that.map,
});
});
}, 0);
return that;
}
});
}
});
答案 0 :(得分:1)
您需要保留对要删除的视图的引用。例如,您有:
var marcadores = new ev.views.Markers(that.map);
您可以通过以下方式保存对视图的引用:
that.marcadores = new ev.views.Markers(that.map);
然后在你的事件处理程序中
teste: function(){
this.marcadores.remove();
},
这会破坏您创建的Marker
视图,但可能无法清除该视图管理的Google地图标记。
答案 1 :(得分:0)
撰写以下内容时:
ev.views.Markers = Backbone.View.extend({ ... });
您正在创建一个新课程。要使ev.views.Markers.remove();
正常工作,ev.views.Markers
需要引用类的实例而不是类本身。
您收到错误Uncaught TypeError: undefined is not a function
,因为ev.views.Markers.remove
不是函数,ev.views.Markers.prototype.remove
是。