【问题标题】:Error destroying Backbone Relational model generated with Coffeescript破坏使用 Coffeescript 生成的主干关系模型时出错
【发布时间】:2011-11-14 23:20:14
【问题描述】:

我猜我遇到的情况必须很容易解决。当然,人们正在使用带有 CoffeeScript 的 Backbone Relational...

这是我的模型:

class MyCompany.Models.Establishment extends Backbone.RelationalModel
  defaults:
    name: null

class MyCompany.Collections.EstablishmentsCollection extends Backbone.Collection
  model: MyCompany.Models.Establishment
  url: '/establishments'

我什至还没有添加任何关系,只是扩展了 RelationalModel。现在通过控制台,当我对模型的实例发出破坏时,它成功地破坏了服务器上的模型,但是完成后它会失败并显示跟踪:

Uncaught TypeError: Object #<Establishment> has no method 'getCollection'
    _.extend.unregister
    Backbone.Events.trigger
    Backbone.RelationalModel.Backbone.Model.extend.trigger
    _.extend.destroy.options.success
    jQuery.extend._Deferred.deferred.resolveWith
    done
    jQuery.ajaxTransport.send.callback

它在主干-relational.js 0.4.0 的第 235 行即将死去,因为我猜“this”是模型,而不是它应该是的模型,并且模型没有方法“getCollection”。

任何想法我做错了什么,或者我应该报告一个错误?作为参考,这里是 Javascript 咖啡生成的:

(function() {
  var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
    for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
    function ctor() { this.constructor = child; }
    ctor.prototype = parent.prototype;
    child.prototype = new ctor;
    child.__super__ = parent.prototype;
    return child;
  };
  MyCompany.Models.Establishment = (function() {
    __extends(Establishment, Backbone.RelationalModel);
    function Establishment() {
      Establishment.__super__.constructor.apply(this, arguments);
    }
    Establishment.prototype.defaults = {
      name: null
    };
    return Establishment;
  })();
  MyCompany.Collections.EstablishmentsCollection = (function() {
    __extends(EstablishmentsCollection, Backbone.Collection);
    function EstablishmentsCollection() {
      EstablishmentsCollection.__super__.constructor.apply(this, arguments);
    }
    EstablishmentsCollection.prototype.model = MyCompany.Models.Establishment;
    EstablishmentsCollection.prototype.url = '/establishments';
    return EstablishmentsCollection;
  })();
}).call(this);

【问题讨论】:

  • 你用什么命令从控制台销毁模型?

标签: coffeescript backbone-relational


【解决方案1】:

您需要更新底层的 Backbone.js 版本。原因如下:

您的错误来自 this 在调用 unregister 时具有错误的值。 unregister 被调用以响应从 register 绑定的事件:

model.bind( 'destroy', this.unregister, this );

第三个参数设置上下文。但该功能直到 Backbone 0.5.2 才添加,正如 changelog 所示。

【讨论】:

  • 正在处理这个问题。我刚刚升级到 0.5.3,但它似乎仍在发生。我正在尝试追踪并确保一切设置正确。
  • 我需要清除 Rails 缓存并重新启动 pow。我在某个地方得到了一些旧的东西。似乎现在正在工作。感谢您帮我追踪,特雷弗。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-21
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多