2011-07-05 6 views
11

M Próbując dać mouseover wydarzenie w moim widzenia kręgosłup, tu jest mój widok:Jak dodać zdarzenie mouseover w backbone.js

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"); 
    } 
}); 

Ale kiedy robię mouseover klasa main nie wykazuje hello world , Proszę zasugerować, co robić?

Próbowałem Heikki Odpowiedź ale mouseover nie działa?

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"); 
    } 
}); 
+0

Zdajesz wiążące we właściwy sposób. Czy sprawdziłeś, że element naprawdę ma przydzieloną główną klasę? –

+0

Ya elementy dostaje główne klasy, nie dostanie się, co to jest problem – XMen

Odpowiedz

13

Zastępujesz główny element widoku, do którego są przypisane zdarzenia.

Spróbuj to zamiast:

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

+0

Witam, wypróbowałem twoją odpowiedź, ale mouseover nie działa, sprawdź, co próbowałem. – XMen

+0

Ahh .. tak. Usuń selektor ze zdarzeń. Dokumenty wspominają o tym: "Pominięcie selektora powoduje powiązanie zdarzenia z elementem głównym widoku (this.el)". Zaktualizowano odpowiedź. – Heikki

+0

dobrze, dzięki, że zadziałało. – XMen