関数をクリックにバインドするイベントがあります。クリックすると、同じビューで別の関数が呼び出されます。残念ながら、スコープは正しいスコープではありません。this.otherFunction()を実行しようとすると、クリックに割り当てられた関数がthis.otherFunction()と同じスコープにありません。otherFunction()のスコープを渡す方法はありますか?
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[objectDOMWindow]にはメソッド'renderLeaveGoal'がありません。これは私が間違ったスコープを保存したように見えますか?