Backbone: Uncaught Error: A "url" property or function must be specified when saving model

Backbone: Uncaught Error: A "url" property or function must be specified when saving model

我确实阅读了很多关于此特定消息的问题,但它们似乎不适用于我的情况。

我有这个型号:

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.urlRoot 参数。

Specify a urlRoot if you're using a model outside of a collection, to enable the default url function to generate URLs based on the model id. "[urlRoot]/id" Normally, you won't need to define this. Note that urlRoot may also be a function.

还要检查您对应用和 app.Templates 的引用是否正确。