2
votes

I have a click event attached to a view and el of that view is defined using className and tagName. After rendering this view, click event is not firing for the className selector.

Below is the my view -

define([
'jquery',
'underscore',
'backbone',
'models/MovieModel',
'text!templates/movieTemplate.html'
],function($,_,Backbone,Moviemodel,movieTemplate){

var Moviesview= Backbone.View.extend({
    tagName:'div',
    className:'box',
    events : {
        'click .box': 'boxClicked'

    },
    boxClicked: function(){ 
           alert('world');
    },
    initialize: function(){

    },
    render: function(){

        var data= {moviemodel: this.model.toJSON(), _:_ };
        var compiledTemplate =_.template(movieTemplate);
        compiledTemplate= compiledTemplate(data);
        this.$el.html(compiledTemplate);
        return this;
    }
});
return Moviesview;
});

This view is getting invoked from another view-

define([
    'jquery',
    'underscore',
    'backbone',
    'models/MovieModel',
    'views/movieview',
    'views/detailedmovieview'
], function ($, _, Backbone, Moviemodel, Movieview, Detailedmovieview) {
var Movieslistview = Backbone.View.extend({

        el: $('.content'),
        initialize: function () {
            var that = this;
            this.collection.fetch({
                success: function (collection, response) {
                    that.render();
                },
                error: function () {
                    alert('failed!');
                }
            });
        },
        render: function () {
            this.collection.each(function (row) {
                var temp = new Movieview({
                        model: row
                    });
                this.$el.append(temp.render().el);

            }, this);
            return this;
        }
    });
return Movieslistview;

});

Template used here-

<div class='main-content'>
 <div class="actors">
   <div class="actors-label">Actors:</div>
   <div class="actors-name"><%= moviemodel.actor_1_name %></div>
 </div>
 <div class="additional-info">               
 </div>
</div>

Click events for .main-content and .additional-info are triggering properly but not for .box .

1

1 Answers

1
votes

In order to attach events to view element, your event binding should be:

events : {
    'click': 'boxClicked'
},