【问题标题】:How to pass collection inside jeditable function?如何在 jeditable 函数中传递集合?
【发布时间】:2012-09-03 07:50:20
【问题描述】:

我想使用 jeditable 编辑我的收藏,其中 modifyCollection 是与事件 dblclick 关联的函数。我有以下代码:

initialize : function(options) {
        view.__super__.initialize.apply(this, arguments);
        this.collection = this.options.collection;
        this.render();
    },

render : function() {
        var template = _.template(tpl, {
            collectionForTemplate : this.collection ,
            });
            this.el.html(template);
            return this;
    },

modifyCollection : function (event){
        $('#name').editable(function(value, settings) {
            return (value);
        }
        , 
           { onblur: function(value) {
                this.modelID=event.target.nameID;
                    this.collection = this.options.collection;

                console.log("This Collection is: " + this.collection); //Shows : undefined
                            //  
                            this.reset(value);
                    $(this).html(value); 
                    return (value); 
            }
        });

想法是更新模型,随后通过 jeditable 更新集合。就地编辑工作正常,但问题是,我无法将集合传递给函数。我想在本地保存对我的集合的所有更改,并在以后将它们发送到服务器。我在这里做错了什么?

【问题讨论】:

  • 嗯,我可以评论的一件事是,this 在您的 onblur() 函数中没有指向 this 集合。尝试在modifyCollection() 函数中添加var self = this;,然后在onblur() 中将this.collection 更改为self.collection
  • @orangewarp:那条评论看起来有点像一个答案,而且是正确的答案。
  • 是的...不是吗? ;-)

标签: backbone.js jeditable


【解决方案1】:

将评论移至正式答案,以防其他人发现此线程。

onblur() 函数中的 this 未指向此集合。尝试在modifyCollection() 函数中添加var self = this;,然后在onblur() 中将this.collection 更改为self.collection,如下所示:

modifyCollection : function (event) {

    var self = this;  // Added this line
    // When working with functions within functions, we need
    // to be careful of what this actually points to.

    $('#name').editable(function(value, settings) {
        return (value);
    }, {
    onblur: function(value) {
        // Since modelID and collection are part of the larger Backbone object,
        // we refer to it through the self var we initialized.
        self.modelID = event.target.nameID;
        self.collection = self.options.collection;

        // Self, declared outside of the function refers to the collection
        console.log("This Collection is: " + self.collection);
            self.reset(value);

            // NOTICE: here we use this instead of self...
            $(this).html(value); // this correctly refers to the jQuery element $('#name')
            return (value); 
        }
    });
});

更新 - 关于自我的预感

@muistooshort 很好地提到了self 实际上是窗口的一个属性,因此如果您不在代码中声明var self = this;,您将指的是一个窗口obj。如果您不确定为什么 self 似乎存在但似乎不起作用,这可能会加重。

这种编码的常见用法倾向于使用that_this 而不是self。你被警告了。 ;-)

【讨论】:

  • 为此使用self 有一个缺点。 window 对象有一个self 成员,因此有一个名为self 的全局变量。当您忘记var self = this; 时会出现问题,您仍然会有self,但它不会是您期望的self。在艰难地学习了这一课之后,我已切换到var _this = this
  • 我认为window.self 仅在您使用框架时才重要。我从来没有真正使用过它,所以我不确定windowwindow.self 之间的区别是什么,可能是古代遗留下来的。当您不想使用 self 时,that_this 似乎是最常见的名称。如果您不知道范围内总是有一个 self,那么点击“self.x() 不是函数”错误会令人抓狂。
  • 如果你不声明var self = this;,我会一直惊恐到下线。我会考虑换。我见过人们使用that 而不是self,这一切都开始变得有意义了。但是问题是,窗口self 与使用window 有何不同?他们似乎在我的控制台中指向完全相同的东西。
  • 陷阱太多了,有点搞笑。我将在答案中添加这个预兆。
猜你喜欢
  • 2023-03-31
  • 2021-07-12
  • 2014-10-26
  • 1970-01-01
  • 1970-01-01
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多