2011-11-22 175 views
3

我是backbone.js n00b,無法理解如何擴展視圖。我有一個基本的「項目」模型和視圖。我想擴展模型和視圖以成爲「特定項目」。有沒有辦法在擴展視圖中添加事件,而不是全部替換它們?在backbone.js中擴展視圖時添加事件處理程序?

查看項目:

var itemView = Backbone.View.extend({ 
    ... 
    events: { 
     "click" : "foo" 
     , "dblclick div": "bar" 
    } 
    ... 
}); 

特定項目視圖:

var specificItemView = itemView.extend({ 
    ... 
    // I'd like this to simply add an event handler not replace the ones defined above 
    events: { 
     "contextmenu" : "baz" 
    } 
    ... 
}); 

甚至它是不是支持擴展這樣的觀點或可我們只做到這一點,以模型?

回答

7

如果我沒有弄錯擴展不能遞歸地工作,但你可以自己做。我覺得這樣的事情應該工作:

var specificItemView = itemView.extend({ 
    ... 
    // I'd like this to simply add an event handler not replace the ones defined above 
    events: _.extend({ 
     "contextmenu" : "baz" 
    }, itemView.prototype.events), 
    ... 
}); 

Here is the proof,擴展不recursivly合併

相關問題