简体   繁体   English

骨干视图 - 根据动态className触发事件

[英]Backbone View - Trigger events depending on a dynamic className

Mates, I've got the following code: 伙计,我有以下代码:

App.Views.Bed = Backbone.View.extend({
tagName: 'li',
template: _.template( App.Templates.Bed ),

events: {
    'click .booked': 'showReservation',
    'click .occupied': 'showGuest',
    'click .free': 'checkIn'
},

render: function(){
    if( this.model.get('id_bookings') ){
        this.clase = 'booked';
    }
    if( this.model.get('id_guests') ){
        this.clase = 'occupied';
    }else{
        this.clase = 'free';
    }
    this.$el.addClass(this.clase).html( this.template( this.model.toJSON() ) );

    return this;
},

checkIn: function(){
    console.log('Check-in form load');
},

showReservation: function(){

},

showGuest: function(){

}

});

I'm trying to trigger a different method depending on the className (which I set depending on the content's model). 我试图触发一个不同的方法取决于className(我根据内容的模型设置)。

Is there a way to filter by classes when defining a view's events? 在定义视图的事件时,有没有办法按类过滤?

Thanks! 谢谢!

In short, it's not possible using the declarative events hash, unless you're willing to do some :parent selector hacks, and I'm not sure if that's even possible, either. 简而言之,使用声明性events哈希是不可能的,除非你愿意做一些:parent选择器黑客,我不确定这是否可能。

The problem is that any jQuery selector you use to define the element (for example the class selector .booked ) is applied within the view´s el , so the element's own class is not considered in the selector. 问题是,任何jQuery选择您使用定义元素(例如类选择.booked )是view's 应用el ,所以元素的自己的类未在选择考虑。

Instead I would dynamically set the handler method. 相反,我会动态设置处理程序方法。 Something like: 就像是:

events: {
    'click': 'onClick'
},

render: function(){
    if( this.model.get('id_bookings') ){
        this.clase = 'booked';
        this.onClick = this.showReservation;
    }
    if( this.model.get('id_guests') ){
        this.clase = 'occupied';
        this.onClick = this.showGuest;
    }else{
        this.clase = 'free';
        this.onClick = this.checkIn;
    }
    _.bindAll(this, 'onClick');

    this.$el.addClass(this.clase).html( this.template( this.model.toJSON() ) );
    return this;
},

Keep it simple. 把事情简单化。 You just need to set one click handler for your button and let it proxy to the right method. 您只需为按钮设置一个单击处理程序,并让它代理到正确的方法。

App.Views.Bed = Backbone.View.extend({
    tagName: 'li',
    template: _.template( App.Templates.Bed ),

    events: {
        'click': 'click_handler'
    },

    render: function(){
        if( this.model.get('id_bookings') ){
            this.clase = 'booked';
        }
        if( this.model.get('id_guests') ){
            this.clase = 'occupied';
        }else{
            this.clase = 'free';
        }
        this.$el.addClass(this.clase).html( this.template( this.model.toJSON() ) );

        return this;
    },

    click_handler: function() {
        if (this.$el.hasClass('booked')) {
            this.showReservation();
        } else if (this.$el.hasClass('occupied')) {
            this.showGuest();
        } else if (this.$el.hasClass('free')) {
            this.checkIn();
        } else {
            // oops!?
        }
    },

    checkIn: function(){
        console.log('Check-in form load');
    },

    showReservation: function(){

    },

    showGuest: function(){

    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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