Understanding Backbone.js event handler

前端 未结 1 1253
夕颜
夕颜 2021-02-10 19:18

So here is my view:

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

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

    events: {
        \'mouseover          


        
相关标签:
1条回答
  • 2021-02-10 19:44

    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
    
    0 讨论(0)
提交回复
热议问题