2

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

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 调用渲染函数。这是那个视图:

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>
4

2 回答 2

3

我在任何地方都没有看到任何_this定义,所以我希望一切都在您调用后立即停止运行renderMap,这将阻止您的事件执行任何操作。

我确实看到了这个:

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

但这不会_this在对象的范围内创建 a,而只是this._this为您的视图实例创建一个默认值;此外,在构建 MapModalView 时,您所做的this可能就是为每个 MapModalView 提供对in的引用。windowwindowthis._this

我认为您所有的_this参考资料都应该是this,也许您想添加:

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

到 MapModalView 的initialize方法,以确保这两个this在您调用它们时始终具有正确性。


现在我们主要是功能代码,现在解决潜在的问题。

主干视图具有el

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

他们有一个$el

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

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

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

但你什么也不做,this.$el所以它仍然指的是 empty <div>。请记住,delegateEvents使用this.$el并没有指向任何有用的东西,因此您的事件不会绑定到 DOM 中的任何内容。

如果你坚持在你的this.el内部设置initialize,那么你也必须设置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/

于 2012-02-22T00:08:03.750 回答
1

试试这个:

render: function() {
    $(this.el).show().append(this.template({
        model: this.model
    }));
    this.renderMap();
    this.renderPin();
    this.delegateEvents();
},
于 2012-02-22T01:01:39.853 回答