javascript - Backbone.js 将对象从 this 转换为 that 并使用每个抛出错误进行调用

标签 javascript backbone.js backbone-views

在我的主干函数中,我为我创建了一个 namespace ,如 var taskListPhraseI = {}; 在使用这个 namespace 时,我的所有函数都被分配了。

当我从集合类中获取集合时,使用我传递给renderBoard的每个函数,在每个函数中,每个函数都不会知道此时的this关键字,我使用了一个声明为该变量并分配给 this 的变量,使用 that 我正在调用该函数,但 render 函数 抛出错误为

TypeError: that.renderBoard is not a function
[Break On This Error]   

that.renderBoard(item)

但我确实有 renderBoard 函数。有给我解决这个问题的线索吗?

我的部分代码:

 taskListPhraseI.allView = Backbone.View.extend({
    el:$('.boardHolder'),
    initialize:function(){
      this.collection = new taskListPhraseI.collection();
      this.collection.fetch({success:this.render});
    },
    render:function(data){
      var that = this;
      _.each(data.models, function(item){
          that.renderBoard(item) // throw the error..
      },this);
    },
    renderBoard:function(item){
      console.log('item'); // i am not getting the call here...
    }
  });

最佳答案

尝试将 this.collection.fetch({success:this.render}); 替换为 this.collection.fetch({success:_.bind(this.render, this )});.

您传递的函数 this.render 没有其上下文,这可以通过 bind 修复在下划线库中。

关于javascript - Backbone.js 将对象从 this 转换为 that 并使用每个抛出错误进行调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14474317/

相关文章:

javascript - Meteor:在 Blaze 中测试两个值相等(例如 {{#if someVar == 'someVal' }})的最佳方法是什么?

javascript - 单击 "Apply"按钮后应用 angularjs 过滤器

backbone.js - 你如何使用 Marionette JS 维护 View 状态?

javascript - 单击时部分隐藏固定页脚

javascript - 在 THREEjs 中对对象的大小进行动画处理

javascript - backbone.js 事件和 el

javascript - 如何从外部文件访问模型 View 内的变量

python - 尝试从 Django 中的 POST 解析 `request.body`

javascript - Backbone.View 事件只触发一次

javascript - 主干 View 事件不会触发