繁体   English   中英

Backbone.js事件未触发

[英]Backbone.js Events not firing

我有一个可以加载的视图。 但是,事件不会触发。 这是视图:

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

})

我删除了一个事件函数,因为它并不重要。 无论如何,此视图已在我的路由器中初始化:

    this.mapModalView = new MapModalView();

我有一个工具提示视图,其中包含一个事件,该事件从ModalMapView调用render函数。 这是该视图:

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;

}

});

我在上面删除了我认为不再重要的功能。

另外,这是我的模板:

<script type="text/template" id="map-modal-template">
<div id="map_modal">
    <button title="Close" id="closeMapModal" class="right">Close</button>
    <h3>Restaurant Map &amp; 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 &amp; Access Info" id="modal_parking">Parking &amp; Access Info</a></li>
    </ul>
    <div id="modalMap"></div>
</div>
</script>

当然是我正在处理的页面上的HTML标记:

<div class="modal_box"></div>

我在任何地方都看不到任何_this定义,因此我希望一切一旦您调用renderMap就会停止运行,这将阻止您的事件执行任何操作。

我确实看到了:

MapModalView = Backbone.View.extend({
    //...
    _this: this,

但这不会在对象范围内创建_this ,而只会为您的视图实例创建默认的this._this 此外,当正在修建MapModalView, this将可能是window因此,所有你正在做的是给每个MapModalView一个参考windowthis._this

我认为您所有的_this引用都应该是this ,也许您想添加:

_.bindAll(this, 'renderMap, 'renderPin');

到MapModalView的initialize方法,以确保这两个始终有正确的this时候,你给他们打电话。


现在,对于潜在的问题,我们主要使用功能代码。

骨干网视图有一个el

所有视图始终都具有DOM元素(el属性),无论它们是否已经插入到页面中。

他们有一个$el

视图元素的缓存jQuery(或Zepto)对象。

this.$el上的delegateEvents方法运算符。 您没有在视图定义中指定el (或idtagName ,...),因此Backbone将el初始化为空的<div> ,然后初始化this.$el = $(this.el) 您的构造函数将更改this.el

this.el = $('.modal_box');

但是您对this.$el不执行任何操作,因此它仍然引用空的<div> 请记住, delegateEvents使用this.$el ,但这并不指向任何有用的东西,因此您的事件不会绑定到DOM中的任何东西。

如果您坚持要在initialize设置this.el ,那么还必须设置this.$el

initialize: function() {
    this.el  = '.modal_box';
    this.$el = $(this.el);
    //...

演示: http//jsfiddle.net/ambiguous/D996h/

或者,您可以使用setElement()为您设置el$el

通常的方法是将el设置为视图定义的一部分:

MapModalView = Backbone.View.extend({
    el: '.modal_box',
    //...

演示: http//jsfiddle.net/ambiguous/NasSN/

或者,您可以在实例化时将el传递给视图:

m = new MapModalView({el: '.modal_box'});
m.render();

演示: http : //jsfiddle.net/ambiguous/9rsdC/

尝试这个:

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.

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