ziz*_*her 0 javascript backbone.js
我在Backbone中有一个具有多种功能的视图.我有的功能是初始化,渲染,回答,answerQuestion,nextQuestion.
这是我在初始化函数中的代码
initialize: function(game) {
_.bindAll(this, 'render', 'answer');
this.render();
}
Run Code Online (Sandbox Code Playgroud)
在render函数中,我通过这样做调用answerQuestion函数:
this.answerQuestion();
Run Code Online (Sandbox Code Playgroud)
它工作正常.
但是在我的回答函数中,我以相同的方式调用nextQuestion函数,我得到了这个错误undefined is not a function,如果我只是在没有this启动时调用函数我得到这个错误'nextQuestion is not defined'
我错过了什么让这个工作.这是完整的答案功能:
var v = $('.question.current .type').find('.input').val();
if (v !== undefined) {
var t = new Date();
var time_spent = t.getTime() - this.t.getTime();
var self = this;
answer.save().done(function(result, status) {
if (status === 'success') {
this.nextQuestion();
}
});
}
Run Code Online (Sandbox Code Playgroud)
你指的是错误的背景:this.nextQuestion();.它应该是self.nextQuestion();.或者您可以将回调绑定到外部函数的上下文,如下所示:
var v = $('.question.current .type').find('.input').val();
if (v !== undefined) {
var t = new Date();
var time_spent = t.getTime() - this.t.getTime();
var self = this;
answer.save().done(function(result, status) {
if (status === 'success') {
this.nextQuestion();
}
}.bind(this));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
587 次 |
| 最近记录: |