破坏用Coffeescript生成的主干关系模型出错



我想,我有一个必须简单解决的情况。当然,人们正在使用骨干关系与CoffeeScript…

我有一个模型:

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

我还没有添加任何关系,只是扩展了RelationalModel。现在,通过控制台,当我对模型的一个实例发出destroy命令时,它成功地在服务器上销毁了模型,但是当完成后,它失败了,并显示如下跟踪信息:

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

它在backbone- relationship .js 0.4.0的第235行死亡,因为"this"是模型,我猜,而不是它应该是什么,模型没有方法"getCollection"。

你知道我做错了什么吗,或者我应该报告一个bug吗?作为参考,下面是咖啡生成的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);

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

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

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

第三个参数设置上下文。但是这个特性直到最近的Backbone 0.5.2才被添加,正如更新日志所示。

相关内容

  • 没有找到相关文章

最新更新