0

我在主干视图中有以下三个功能。最初,对 inside 和函数的调用updateTimer触发PlaybackStartRecording一个错误,在我使用 调用之前,该错误就消失了this,如 this.updateTimer. 但是,我现在已经在生产服务器上放置了一个演示应用程序(代码被编译到一个文件中)并且对 this.updateTimer 的调用触发了一个错误

 Uncaught TypeError: Object #<Object> has no method 'updateTimer' (repeated 28 times)

你能解释一下为什么会这样吗?

在我的本地机器上工作的三个功能:

     PlayBack: function(e){
         e.preventDefault();
        this.updateTimer(0);
        SC.recordPlay({
            progress: function(ms) {
                this.updateTimer(ms);
            }
        });

      },

      updateTimer: function(ms){
         $('.status').text(SC.Helper.millisecondsToHMS(ms));


      },

      StartRecording: function(e){

         $('#startRecording').hide();
          $('#stopRecording').show();
          e.preventDefault();
          SC.record({
            progress: function(ms, avgPeak) {
              this.updateTimer(ms);
            }
          });

      },

从生产服务器的控制台

,PlayBack: function(t) {
        t.preventDefault(), this.updateTimer(0), SC.recordPlay({progress: function(t) {
                this.updateTimer(t)
            }})
    },updateTimer: function(t) {
        $(".status").text(SC.Helper.millisecondsToHMS(t))
    },StartRecording: function(t) {
        $("#startRecording").hide(), $("#stopRecording").show(), t.preventDefault(), SC.record({progress: function(t) {
                this.updateTimer(t)
Uncaught TypeError: Object #<Object> has no method 'updateTimer' (repeated 28 times)
            }})
4

1 回答 1

3

您收到错误是因为this上下文在您传递给的内部函数中丢失SC.record并且不再引用您的 Backbone 视图。有几种方法可以解决这个问题,其中一种是利用闭包并创建对正确的引用并在回调this中使用它:progress

  StartRecording: function(e){
     var self = this;
     $('#startRecording').hide();
      $('#stopRecording').show();
      e.preventDefault();
      SC.record({
        progress: function(ms, avgPeak) {
          self.updateTimer(ms);
        }
      });

  },

updateTimerin 的调用SC.recordPlay也是如此Playback

于 2013-08-12T19:06:38.167 回答