0

我猜我的情况必须很容易解决。当然,人们正在使用带有 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);
4

1 回答 1

1

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

您的错误来自调用this时的错误值unregisterunregister被调用以响应从 绑定的事件register

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

第三个参数设置上下文。但正如更新日志显示的那样,该功能直到 Backbone 0.5.2 才添加。

于 2011-11-14T15:20:14.167 回答