Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add mouseover event in Backbone.js

M Trying to give mouseover event in my view of Backbone, here is my view :

Backbone.View.extend({
  template :_.template( '<li class="<% if (refertype=="U"){%>info <% }else{%> access<%}%> main"><%=refername%>'+

            '</li>'),
  initialize: function() {
    _.bindAll(this, 'render', 'close');
    this.model.bind('change', this.render);
    this.model.view = this;
  },
    events: {
        "mouseover .main": "mouseovercard"
    },
  // Re-render the contents of the Card item.
  render: function() {
    this.el=this.template(this.model.toJSON());
    $(".cards-list").append(this.el);
  },
    mouseovercard: function() {
        console.log("hello world");
    }
});

But when I am doing mouseover the main class it is not showing hello world, Please suggest what to do?

Tried Heikki Answer but mouseover not working ?

App.Backbone.CardView = Backbone.View.extend({
    tagName: 'li',
    className: 'main',
  initialize: function() {
    _.bindAll(this, 'render');
    this.model.bind('change', this.render);
    this.model.view = this;
  },
    events:{
        "mouseover .main": "mouseovercard"
    },
  // Re-render the contents of the Card item.
  render: function() {
         $(this.el)
            .removeClass('info access')
            .addClass(this.model.get('refertype') == 'U' ? 'info' : 'access')
            .text(this.model.get('refername'));
    $(".cards-list").append(this.el);
  },
    mouseovercard: function() {
        console.log("hello world");
    }
});
like image 316
XMen Avatar asked Jul 05 '11 12:07

XMen


1 Answers

You are replacing the view's root element where the events are bound to.

Try this instead:

Backbone.View.extend({

    tagName: 'li',

    className: 'main',

    events: {
        'mouseover': 'mouseovercard'
    },

    initialize: function() {
        _.bindAll(this, 'render');
        this.model.bind('change', this.render);
    },

    render: function() {
        $(this.el)
            .removeClass('info access')
            .addClass(this.model.get('refertype') == 'U' ? 'info' : 'access')
            .text(this.model.get('refername'));
        return this;
    },

    mouseovercard: function() {
        console.log('hello world');
    }

});

http://documentcloud.github.com/backbone/#View-extend

like image 163
Heikki Avatar answered Nov 07 '22 09:11

Heikki