【问题标题】:backbone.js illegal invocation骨干网.js非法调用
【发布时间】:2013-01-18 11:23:18
【问题描述】:

http://jsfiddle.net/herrturtur/ExWFH/

试试这个:

  1. 点击加号按钮,
  2. 双击新创建的名称字段,
  3. 为字段填写一些内容
  4. 按 Enter。

时代字段(从和直到)正确显示,但没有保存(重新加载以查看效果)并且名称字段根本没有更新。

在第 153 行对 NameView 的关闭函数(由 Enter 键触发)中的属性进行 set() 处理后,我调用 this.model.save()

close: function(e){ 
    this.$el.removeClass('editing');
    this.model.set({
        value: this.$('.name-value').val(), 
        from: this.model.from, // this is saved in the 
        until: this.model.until
    });

    this.model.save();

    if(this.eraView.$el.attr('editing')){
        this.eraView.close();
    }
    this.render();
},

谁能告诉我我做错了什么?

最重要的是,我得到一个 TypeError: Illegal Invocation in Chrome。

【问题讨论】:

    标签: javascript backbone.js local-storage


    【解决方案1】:

    您遇到的问题是在调用 close 函数时。

    在您的 close 函数中,您正在调用自己,因此它看起来像是一个冗余循环。

    JS Bin

    在您的karass.EraView 上,您还在函数中触发了相同的函数,这是不必要的。我建议将其重命名为与操作意图相对应的不同函数。

    this.trigger('close');
    

    【讨论】:

      【解决方案2】:

      由于以下原因,您会收到该错误: this.eraView.on('close', close);

      应该是: this.eraView.on('close', this.close, this);

      接下来,当你这样做时:

      this.model.set({
                  value: this.$('.name-value').val(), 
                  from: this.model.from, 
                  until: this.model.until
              });
      

      this.model.fromthis.model.until 是未定义的,而且您之前已经设置了这些属性。该模型已经拥有它们。如果您想访问它们,您需要使用this.model.get('from') 而不是this.model.from。但是从模型中获取值并再次设置它是不必要的。您可以只使用set 值,因为其他 2 个已经存在。

      另一个问题类似。当你渲染 NameView 时,你正在做:

      this.$el.html(this.template({value: this.model.value}));

      和以前一样,你需要改为this.model.get('value')

      您应该尝试在 Chrome 中设置一些断点并逐步执行代码。很容易通过它来看看出了什么问题:)

      更新jsFiddle

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-17
        • 2012-12-02
        • 1970-01-01
        • 1970-01-01
        • 2012-11-18
        • 2012-11-23
        相关资源
        最近更新 更多