Backbone.js Mustache lambda函数访问其视图实例';这';
mustache lambda函数是否可以访问其视图实例Backbone.js Mustache lambda函数访问其视图实例';这';,backbone.js,mustache,Backbone.js,Mustache,mustache lambda函数是否可以访问其视图实例this Backbone.View.extend ({ initialize: function (options) { this.collection = options.collection // large backbone.collection }, parseContent: function (){ return function (id, render){
this
Backbone.View.extend ({
initialize: function (options) {
this.collection = options.collection // large backbone.collection
},
parseContent: function (){
return function (id, render){
//this.collection is undefined below
return this.collection.get (render (id)).get ('stuff);
}
}
});
尝试了.bind(this.parseContent,this)
内部initialize()
,此
仍然在parseContent()
内部携带模型上下文
我当前的解决方法是将此.collection
保存到我的应用程序根命名空间中,并从中进行访问。想知道有没有一种更干净的方法来达到上述目的
感谢您的建议。如果要传递由
parseContent
返回的函数,您应该
initialize
中使用,在parseContent
中对每个实例强制执行this
Backbone.View.extend ({
initialize: function (options) {
_.bindAll(this, 'parseContent');
// you don't need this.collection = options.collection
// collection is part of the special variables handled By Backbone
},
parseContent: function (){
var f = function (id, render){
console.log(this.collection);
}
return _.bind(f, this);
}
});
还有一个演示如果要传递由
parseContent
返回的函数,您应该
initialize
中使用,在parseContent
中对每个实例强制执行this
Backbone.View.extend ({
initialize: function (options) {
_.bindAll(this, 'parseContent');
// you don't need this.collection = options.collection
// collection is part of the special variables handled By Backbone
},
parseContent: function (){
var f = function (id, render){
console.log(this.collection);
}
return _.bind(f, this);
}
});
还有一个演示
这个集合
里面的f
函数是未定义的
@chengpingon你能设置一个演示吗?因为这把小提琴按预期工作。你的建议是正确的。实际上,我使用的是Backbone.marionete,上面的上下文是一个ItemView
呈现表中的一行,在CompositeView
中换行。尽管如此,您的解决方案仍然有效,但我仍然得到undefined
内部函数f
这个。集合内部f
函数是undefined
@chengpingon您可以设置一个演示吗?因为这把小提琴按预期工作。你的建议是正确的。实际上,我使用的是Backbone.marionete,上面的上下文是一个ItemView
呈现表中的一行,在CompositeView
中换行。尽管如此,您的解决方案应该可以工作,但我仍然得到函数f
内部的undefined
。