我猜想,我的情况必须简单易行。当然人们正在使用Backbone Relational和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
它在bone-relational.js 0.4.0的第235行中死亡,因为“这个”是模型,我想,而不是它应该是什么,并且模型没有方法“getCollection”。 / p>
任何想法我做错了,还是应该报告错误?作为参考,这里是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);
答案 0 :(得分:1)
您需要更新底层的Backbone.js版本。原因如下:
当this
被调用时,您的错误来自unregister
错误的值。调用unregister
以响应来自register
的事件:
model.bind( 'destroy', this.unregister, this );
第三个参数设定上下文。但是这个功能最近才被添加到Backbone 0.5.2,正如changelog所示。