Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Backbone.js 从方法';s观点_Backbone.js - Fatal编程技术网

Backbone.js 从方法';s观点

Backbone.js 从方法';s观点,backbone.js,Backbone.js,我在访问视图方法上的集合时遇到了一个问题,事实上,它在initialize()方法中工作得很好,但我创建了另一个(drawVisualization()),当我尝试访问此集合时,出现了一个未定义的错误。集合,这可能是一个愚蠢的问题,但我没有找到任何解决方案,我尝试在initialize方法上使用u.bind,但在这种情况下似乎不起作用,下面是代码: App.Views.account = Backbone.View.extend({ className: 'account', el: $('

我在访问视图方法上的集合时遇到了一个问题,事实上,它在initialize()方法中工作得很好,但我创建了另一个(drawVisualization()),当我尝试访问此集合时,出现了一个未定义的错误。集合,这可能是一个愚蠢的问题,但我没有找到任何解决方案,我尝试在initialize方法上使用u.bind,但在这种情况下似乎不起作用,下面是代码:

App.Views.account = Backbone.View.extend({

className: 'account',

el: $('#account-container'),

initialize: function(){
    console.log(this.collection.toJSON()); //Works fine !
    this.template = _.template($('#account-template').html());
    _.bind(this.drawVisualization, this); //Seems to be useless
},

render: function(){
//Some code...
    return this;
},

drawVisualization: function(){
      console.log(this.collection.toJSON()); //Fail because of undefined collection !
}

谢谢你的帮助

我不知道为什么它不起作用,但请尝试使用下划线的
bindAll

initialize: function(){
  _.bindAll(this);  
  this.template = _.template($('#account-template').html());
} 

对我来说,在每个视图的开头转储
bindAll
初始化
是避免此类问题的好方法。

我不知道为什么它不起作用,但尝试使用下划线的
bindAll

initialize: function(){
  _.bindAll(this);  
  this.template = _.template($('#account-template').html());
} 

对我来说,在每个视图的开头转储
bindAll
initialize
是避免此类问题的好方法。

在主干中,您不需要将
绑定到主干视图的方法

所以试着跳过这部分

\uuu.bind(this.drawVisualization,this)//似乎没有用

而是像这样绑定这个

this.collection.bind("reset", this.drawVisualization, this); 

在主干中,您不需要将
绑定到主干视图的方法

所以试着跳过这部分

\uuu.bind(this.drawVisualization,this)//似乎没有用

而是像这样绑定这个:

this.collection.bind("reset", this.drawVisualization, this); 

因为它是属于集合的模型,所以您可以实际执行此操作。
model.collection
试用它因为它是属于集合的模型,所以您可以实际执行此操作。
model.collection
试用它