Skip to content Skip to sidebar Skip to footer

Toggle Class On Mouse Click Event

I've got a Backbone.View that renders a collection and filters it on mouse click. I need to add class active to the button that I click, but the problem is that buttons are the par

Solution 1:

I simplified the rendering and added some optimizations.

Since we don't have your template, I changed it to enable optimization:

<button id="active" type="button">Active</button>
<button id="passed" type="button">Passed</button>
<div class="list"></div>

Then your list view could be like this:

var ResumeList = Backbone.View.extend({
    events: {
        'click #active': 'showActive',
        'click #passed': 'showPassed'
    },
    initialize: function() {
        this.childViews = [];
        this.collection = new ResumeCollection();
    },

    render: function() {
        this.$el.html(this.template());
        // cache the jQuery element once
        this.elem = {
            $list: this.$('.list'),
            $active: this.$('#active'),
            $passed: this.$('#passed')
        };
        this.renderList(); // default list rendering
        return this;
    },

    renderList: function(collection) {
        this.elem.$list.empty();
        this.removeChildren();
        collection = collection || this.collection.models;

        // Underscore's 'each' has a argument for the context.
        _.each(collection, this.renderItem, this);
    },
    renderItem: function(model) {
        var view = new ResumeView({ model: model });
        this.childViews.push(view);
        this.elem.$list.append(view.render().el);
    },

    showActive: function() {
        this.elem.$active.toggleClass('active');

        var filtered = this.collection.filterActive();
        this.renderList(filtered);
    },

    /**
     * Gracefully call remove for each child view.
     * This is to avoid memory leaks with listeners.
     */
    removeChildren: function() {
        var view;
        while ((view = this.childViews.pop())) {
            view.remove();
        }
    },
});

Additional information:


Solution 2:

I have edited the snippet can you try this.

var ResumeList = Backbone.View.extend({
             events: {
                 'click #active': 'filterActive',
                 'click #passed': 'showPassed'
             },

             toggleElement: undefined,

             initialize: function () {
                 this.collection = new ResumeCollection();
             },

             render: function (filtered) {
                 var self = this;
                 var data;
                 if (!filtered) {
                     data = this.collection.toArray();
                 } else {
                     data = filtered.toArray();
                 }
                 this.$el.html(this.template({ collection: this.collection.toJSON() });

                 _.each(data, function (cv) {
                     self.$el.append((new ResumeView({model: cv})).render().$el);  
                 });
                 return this;    
             },

             filterActive: function (evt) {

                 this.toggleElement = this.$el.find(evt.currentTarget);
                 // a function that returns a new filtered collection
                 var filtered = this.collection.filterActive();
                 this.render(filtered);
                 this.toggleActive();
             },

             toggleActive: function() {

                 if(this.toggleElement.is(':checked')) {
                   this.$el.find('#active').addClass('active');
                 } else {
                   this.$el.find('#active').removeClass('active');
                 }
             }
        });

Please note: I have taken checkbox element instead of button.


Post a Comment for "Toggle Class On Mouse Click Event"