Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Backbone.js event handler

So here is my view:

$(function() {
var ImageManipulation = Backbone.View.extend({

    el: $('body'),
    tagName: "img",

    events: {
        'mouseover img': 'fullsize',
        'click img#current': 'shrink'
    },
    initialize: function() {
        _.bindAll(this, 'render', 'fullsize', 'shrink');

        //var message = this.fullsize;
        //message.bind("test", this.fullsize);
    },
    render: function() {

    },
    fullsize: function() {
        console.log("in fullsize function");
        console.log(this.el);
        $('.drop-shadow').click(function() {
            console.log(this.id);
            if (this.id != 'current') {
                $('.individual').fadeIn();
                $(this).css('position', 'absolute');
                $(this).css('z-index', '999');
                $(this).animate({
                    top: '10px',
                    height: '432px',
                }, 500, function() {
                    this.id = "current";
                    console.log("animation complete");
                    return true;
                });
            };
        });
    },
    shrink: function() {
        $('.individual').fadeOut();
        $('#current').animate({
            height: '150px',
        }, 500, function() {
            this.id = "";
            $(this).css('position', 'relative');
            $(this).css('z-index', '1');
            console.log("animation complete");
            return true;
        });
    }
});
var startImages = new ImageManipulation();
});

What I don't understand is how to change the el to make 'this' take over the click function I have in full-size. I would much rather have the click jQuery function removed and have the mouseover function be another click, but I cant seem to figure out how to assign 'this' to the particular image that is being clicked. I hope my question makes sense.

like image 365
thatmiddleway Avatar asked Dec 04 '22 20:12

thatmiddleway


1 Answers

Backbone's event handler assumes that you want to know about the object (both its code, and its DOM representation, the View.el object) for every event, and that the event is intended to change some aspect of the view and/or model. The actual target of the click is something you're assumed to know, or assumed to be able to derive.

Derivation is rather simple:

fullsize: function(ev) {
    target = $(ev.currentTarget);

And replace all your this. references within your call to target.. this. will continue to refer to the View instance. In your inner function, the anonymous one assigned to .drop-shadow, this. will refer to the object that was just clicked on. If you want access to the surrounding context, use the closure forwarding idiom:

fullsize: function(ev) {
    var target = ev.currentTarget;
    var self = this;
    $('.drop-shadow').click(function(inner_ev) {
        console.log(this.id);  // the same as inner_ev.currentTarget
        console.log(self.cid); // the containing view's CID
like image 110
Elf Sternberg Avatar answered Dec 08 '22 14:12

Elf Sternberg