我正在尝试保存模型,我可以保存第一个模型,但是如果我保存一个模型然后另一个直接 - 我得到以下错误,
Uncaught TypeError: Object [object Object] has no method 'call'
导致此错误的原因是什么?
我的保存代码如下所示,
GroupModalHeaderView.prototype.save = function(e) {
var $this = this;
this.collection = new app.GroupCollection();
if(this.$("#group-name").val() !== "") {
this.collection.add(this.model,{
merge: true
});
}
this.model.save({
name: this.$("#group-name").val(),
async: false,
wait: true
}, {
success: function() {
var GroupList = new app.GroupListView({
model: $this.model,
collection: $this.collection
});
var modal = new app.GroupModalView({
model: $this.model,
collection: $this.collection
});
$this.collection.on("change", GroupList.render(), this);
$this.collection.on("change", modal.render(), this);
//return $this.cancel();
}
});
};
我的模型代码看起来像这样,
(function() {
var Group, GroupSearchModel,
__hasProp = {}.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; };
Group = (function(_super) {
__extends(Group, _super);
function Group() {
return Group.__super__.constructor.apply(this, arguments);
}
Group.prototype.urlRoot = config.base + "api/group/groups";
Group.prototype.defaults = {
user_id: "",
name: "New Group",
email: "",
url: "",
telephone: "",
mobile: "",
fax: "",
people: ""
};
Group.prototype.idAttribute = "id";
return Group;
})(app.BaseModel);
GroupSearchModel = (function(_super) {
__extends(GroupSearchModel, _super);
function GroupSearchModel() {
return GroupSearchModel.__super__.constructor.apply(this, arguments);
}
return GroupSearchModel;
})(app.BaseModel);
this.app = window.app || {};
this.app.Group = Group;
this.app.GroupSearchModel = GroupSearchModel;
}).call(this);
我认为这与模型末尾的.call(this)
有关?
答案 0 :(得分:1)
你的代码中有一堆蠢事,但我的第一个猜测是这个错误导致你当前的问题。绑定事件处理程序时,通常需要引用而不是EXCUTE 处理函数。因此,不是GroupList.render()
(使用括号)执行函数,而是需要GroupList.render
(没有括号)
只是在没有执行的情况下引用它。
$this.collection.on("change", GroupList.render, this);
$this.collection.on("change", modal.render, this);
//return $this.cancel();