我有一個事件將一個函數綁定到點擊。點擊在同一視圖中調用另一個功能。不幸的是,範圍不是正確的範圍。當我嘗試執行this.otherFunction()時,分配給click的函數與this.otherFunction()不在同一個作用域中。有沒有辦法通過其他函數()的範圍?如何將'this'傳遞給使用骨幹的事件?
initialize: function() {
this.render();
if (joinedGoalList.get(this.model.id) != null) {
this.renderLeaveGoal();
} else {
this.renderJoinGoal();
}
},
events: {
"keypress #goal-update": "createOnEnter",
"click #join-goal": "joinGoal",
"click #leave-goal": "leaveGoal",
},
joinGoal: function() {
matches = joinedGoalList.where({id: this.model.get("id")});
if (matches.length == 0) {
joinedGoalList.create({goal_id: this.model.get("id")}, {wait: true, success: function() {
var self = this;
self.renderLeaveGoal();
}, error: function() {
console.log("error");
}});
}
},
renderLeaveGoal: function() {
console.log("render leave goal");
var template = _.template($("#leave-goal-template").html());
console.log(template);
$("#toggle-goal-join").html(template());
},
這些都是在相同的看法。
編輯: 嗯,現在的問題是,我得到這個錯誤: Uncaught TypeError:Object [對象DOMWindow]沒有方法'renderLeaveGoal'。這似乎是我保存了錯誤的範圍?
你可以添加一些你的代碼嗎? – ataddeini 2012-04-21 02:10:25