我现在正在开发应用程序,当我创建一个组时,保存功能很好,模型被添加到集合中,并保存到数据库中,但是如果我想编辑我刚刚创建的组,当我点击保存时创建新模型(和 POST 请求),而不是编辑数据并触发 PUT 请求。这是我的保存功能 - 在编辑现有模型时我没有触发 PUT 请求有什么原因吗?
GroupModalHeaderView.prototype.save = function(e) {
var $collection, $this;
if (e) {
e.preventDefault();
}
$this = this;
if (this.$("#group-name").val() !== "") {
$collection = this.collection;
if (this.model.isNew()) {
this.collection.add(this.model);
}
return this.model.save({ name: this.$("#group-name").val()}, {
async: false,
wait: true,
success: function() {
var modelID = $this.model.get('id');
return this.modal = new app.GroupModalView({
model: $this.collection.get(modelID),
collection: $this.collection
});
}
});
}
};
这是我的模型默认值,
Group.prototype.defaults = {
user_id: "",
name: "New Group",
email: "",
url: "",
telephone: "",
mobile: "",
fax: "",
people: ""
};
this.model
这是保存之前的console.log ,
Group {cid: "c116", attributes: Object, _changing: false, _previousAttributes: Object, changed: Object…}
_changing: false
_events: Object
_pending: false
_previousAttributes: Object
email: ""
fax: ""
mobile: ""
name: "New Group"
people: ""
telephone: ""
url: ""
user_id: ""
wait: true
__proto__: Object
attributes: Object
changed: Object
cid: "c116"
collection: GroupCollection
id: 189
__proto__: ctor