2013-03-21 33 views
0

我在Backbone工作。在View中工作,我想從另一個方法中調用一個方法。Backbone.js:從視圖中的另一個方法調用一個方法?

events: { 
    "click span": "updateURL", 
    "click .tag": "clearTag" 
}, 
updateURL: function() { 
    // do stuff 
}, 
clearTag: function(e) { 
    console.log(this); 
    // this fails 
    this.updateURL(); 
}, 

但是thisclearTag似乎被綁定到的元素,並且updateURL不被調用。有沒有辦法從clearTag以內撥打updateURL

+1

通過事件結構創建的事件自動將此上下文綁定到視圖。你確定在調用this.updateURL時沒有其他代碼正在失去「this」上下文嗎? – explunit 2013-03-21 16:45:46

+0

你說得很對 - 我很愚蠢。感謝您的幫助,我會要求結束這個問題。 – Richard 2013-03-21 17:06:28

回答

2

從有關視圖事件綁定的骨幹來源:

// Callbacks will be bound to the view, with `this` set properly. 
// Uses event delegation for efficiency. 
// Omitting the selector binds the event to `this.el`. 
// This only works for delegate-able events: not `focus`, `blur`, and 
// not `change`, `submit`, and `reset` in Internet Explorer. 

這樣的背景下應該在這裏你的觀點,所以別的東西正在發生。你能提供一個小提琴嗎?