[英]Backbone.js Events not firing
I have a view I am able to load. 我有一个可以加载的视图。 However the events do not fire.
但是,事件不会触发。 Here is the view:
这是视图:
MapModalView = Backbone.View.extend({
events: {
'click #closeMapModal': 'closeModal',
'click #modal_streetview': 'renderStreet'
},
initialize: function(){
this.el = $('.modal_box');
this.template = _.template($('#map-modal-template').html());
_.bindAll(this, 'renderMap', 'renderPin');
},
render: function(){
$(this.el).show().append(this.template({
model: this.model
}));
this.renderMap();
this.renderPin();
},
renderMap: function(){
this.thisLat = _this.model.get('lat');
this.thisLng = _this.model.get('lng');
this.modalMap = new google.maps.Map(document.getElementById('modalMap'), {
zoom : 12,
center : new google.maps.LatLng(this.thisLat, this.thisLng), // VANCOUVER
mapTypeId : google.maps.MapTypeId.ROADMAP
});
},
renderPin: function(){
marker = new google.maps.Marker({
position : new google.maps.LatLng(this.thisLat, this.thisLng),
map : this.modalMap,
animation : google.maps.Animation.DROP,
icon : '/img/lazy-pin.png'
});
},
renderStreet: function(e){
e.preventDefault();
this.modalMap.getStreetView().setPosition(this.modalMap.center);
this.modalMap.getStreetView().setVisible(true);
},
closeModal: function(e){
alert('hello');
e.preventDefault();
$(this.el).hide().find('div').remove();
}
}) })
I removed one of the event functions because it wasn't important. 我删除了一个事件函数,因为它并不重要。 Anyways, this view is initialized in my router:
无论如何,此视图已在我的路由器中初始化:
this.mapModalView = new MapModalView();
And I have a tooltip view, which has an event inside of it, that calls the render function from ModalMapView. 我有一个工具提示视图,其中包含一个事件,该事件从ModalMapView调用render函数。 Here is that View:
这是该视图:
ToolTipView = Backbone.View.extend({
initialize: function() {
this.template = _.template($('#toolTip').html());
this.mapTemplate = _.template($('#map-modal-template').html());
this.model.bind('change:tooltip', this.toggleTooltip, this);
return this;
},
events: {
'mouseenter': 'toolTipOn',
'mouseleave': 'toolTipOff',
'click #show-restaurant-map': 'mapModal'
},
mapModal: function(){
router.mapModalView.render(this.model);
},
render: function() {
$(this.el).html(this.template({
model: this.model
}));
this.delegateEvents();
return this;
}
}); });
I removed the functions that I didnt think would be important again above. 我在上面删除了我认为不再重要的功能。
Also, here is my template: 另外,这是我的模板:
<script type="text/template" id="map-modal-template">
<div id="map_modal">
<button title="Close" id="closeMapModal" class="right">Close</button>
<h3>Restaurant Map & Access Information</h3>
<ul>
<li><a href="#" title="Map View" id="modal_mapview">Map</a></li>
<li><a href="#" title="Street View" id="modal_streetview">Street View</a></li>
<li><a href="#" title="Parking & Access Info" id="modal_parking">Parking & Access Info</a></li>
</ul>
<div id="modalMap"></div>
</div>
</script>
and of course, my HTML markup on the page I am working on: 当然是我正在处理的页面上的HTML标记:
<div class="modal_box"></div>
I don't see any _this
defined anywhere so I would expect everything to stop running as soon as you call renderMap
and that would prevent your events from doing anything. 我在任何地方都看不到任何
_this
定义,因此我希望一切一旦您调用renderMap
就会停止运行,这将阻止您的事件执行任何操作。
I do see this: 我确实看到了:
MapModalView = Backbone.View.extend({
//...
_this: this,
but that doesn't create a _this
in the object's scope, that just creates a default this._this
for your view instances; 但这不会在对象范围内创建
_this
,而只会为您的视图实例创建默认的this._this
。 furthermore, when MapModalView is being built, this
will probably be window
so all you're doing is giving each MapModalView an reference to window
in this._this
. 此外,当正在修建MapModalView,
this
将可能是window
因此,所有你正在做的是给每个MapModalView一个参考window
中this._this
。
I think all your _this
references should be this
and perhaps you want to add: 我认为您所有的
_this
引用都应该是this
,也许您想添加:
_.bindAll(this, 'renderMap, 'renderPin');
to MapModalView's initialize
method to make sure those two always have the right this
when you call them. 到MapModalView的
initialize
方法,以确保这两个始终有正确的this
时候,你给他们打电话。
And now for the underlying problem now that we have mostly functional code. 现在,对于潜在的问题,我们主要使用功能代码。
Backbone views have an el
: 骨干网视图有一个
el
:
All views have a DOM element at all times (the el property), whether they've already been inserted into the page or not.
所有视图始终都具有DOM元素(el属性),无论它们是否已经插入到页面中。
and they have an $el
: 他们有一个
$el
:
A cached jQuery (or Zepto) object for the view's element.
视图元素的缓存jQuery(或Zepto)对象。
The delegateEvents
method operators on this.$el
. this.$el
上的delegateEvents
方法运算符。 You don't specify an el
(or id
, tagName
, ...) as part of your view's definition so Backbone initializes your el
to an empty <div>
and then initializes this.$el = $(this.el)
. 您没有在视图定义中指定
el
(或id
, tagName
,...),因此Backbone将el
初始化为空的<div>
,然后初始化this.$el = $(this.el)
。 Your constructor changes this.el
: 您的构造函数将更改
this.el
:
this.el = $('.modal_box');
but you don't do anything with this.$el
so it still refers to the empty <div>
. 但是您对
this.$el
不执行任何操作,因此它仍然引用空的<div>
。 Remember that delegateEvents
uses this.$el
and that's not pointing at anything useful so your events don't get bound to anything in the DOM. 请记住,
delegateEvents
使用this.$el
,但这并不指向任何有用的东西,因此您的事件不会绑定到DOM中的任何东西。
If you insist on setting this.el
inside your initialize
, then you'll have to set this.$el
as well: 如果您坚持要在
initialize
设置this.el
,那么还必须设置this.$el
:
initialize: function() {
this.el = '.modal_box';
this.$el = $(this.el);
//...
Demo: http://jsfiddle.net/ambiguous/D996h/ 演示: http : //jsfiddle.net/ambiguous/D996h/
Or you could use setElement()
to set el
and $el
for you. 或者,您可以使用
setElement()
为您设置el
和$el
。
The usual approach is to set el
as part of the view's definition: 通常的方法是将
el
设置为视图定义的一部分:
MapModalView = Backbone.View.extend({
el: '.modal_box',
//...
Demo: http://jsfiddle.net/ambiguous/NasSN/ 演示: http : //jsfiddle.net/ambiguous/NasSN/
Or you can pass the el
to the view when you instantiate it: 或者,您可以在实例化时将
el
传递给视图:
m = new MapModalView({el: '.modal_box'});
m.render();
Demo: http://jsfiddle.net/ambiguous/9rsdC/ 演示: http : //jsfiddle.net/ambiguous/9rsdC/
Try this: 尝试这个:
render: function() {
$(this.el).show().append(this.template({
model: this.model
}));
this.renderMap();
this.renderPin();
this.delegateEvents();
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.