2011-07-05 33 views
11

M Đang cố gắng để cung cấp cho mouseover sự kiện theo quan điểm của tôi về Backbone, đây là quan điểm của tôi:Làm thế nào để thêm sự kiện mouseover trong 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"); 
    } 
}); 

Nhưng khi tôi đang làm mouseover lớp main nó không hiển thị hello world , Xin đề nghị làm gì?

Đã thử Heikki Trả lời nhưng di chuột không hoạt động?

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

Bạn ràng buộc một cách đúng đắn. Bạn đã kiểm tra phần tử đó có thực sự được chỉ định lớp chính không? –

+0

Ya yếu tố là nhận được các lớp học chính, không nhận được vấn đề là gì – XMen

Trả lời

13

Bạn đang thay thế phần tử gốc của chế độ xem nơi các sự kiện được liên kết.

Hãy thử điều này thay vì:

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

Hi, đã thử câu trả lời của bạn nhưng mouseover không làm việc, xin vui lòng kiểm tra câu hỏi cho những gì tôi đã cố gắng. – XMen

+0

Ahh .. vâng. Xóa bộ chọn khỏi các sự kiện. Tài liệu đề cập đến điều này: "Bỏ qua bộ chọn khiến sự kiện bị ràng buộc với phần tử gốc của khung nhìn (this.el)". Đã cập nhật câu trả lời. – Heikki

+0

okay tuyệt vời nhờ nó hoạt động. – XMen

Các vấn đề liên quan