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
Backbone.js Mustache lambda函数访问其视图实例';这';_Backbone.js_Mustache - Fatal编程技术网

Backbone.js 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){

mustache lambda函数是否可以访问其视图实例
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