【发布时间】:2015-01-30 12:32:13
【问题描述】:
我确实阅读了很多关于此特定消息的问题,但它们似乎不适用于我的情况。
我有这个模型:
app.Template = Backbone.Model.extend({
defaults: {
name: '',
templateType: null,
content: '',
defaultOfType: false,
createdAt: null,
updatedAt: null
}
});
这个集合:
var Templates = Backbone.Collection.extend({
model: app.Template,
url: '/templates'
});
app.Templates = new Templates();
在我的路由器中:
this.view = new app.TemplateFormView({ model: new app.Template() });
在那个观点中:
initialize: function() {
this.listenTo(app.Templates, 'add', this.editTemplate);
app.Templates.add(this.model);
// rendering function, other stuff
},
saveMyTemplate: function(e) {
e.preventDefault();
var formData = {};
var oldData = this.model.previousAttributes();
$(e.target).closest("form").find(":input").each(function() {
var el = $(this);
formData[el.attr("id")] = el.val();
});
this.model.set(formData).save();
}
当我尝试保存模型时,我收到此错误:
Uncaught Error: A "url" property or function must be specified
如您所见,url 设置在集合中,模型是集合的一部分。知道为什么该模型似乎不是该集合的成员吗?
谢谢!
【问题讨论】:
-
您在哪里以及如何执行 model.save()?
-
当然 - 我将方法编辑到我的问题中!
标签: backbone.js