fiiv
fiiv

Reputation: 1287

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

I did read a lot of the questions regarding this particular message, but they don't seem to apply in my case.

I have this model:

app.Template = Backbone.Model.extend({
  defaults: {
    name: '',
    templateType: null,
    content: '',
    defaultOfType: false,
    createdAt: null,
    updatedAt: null
  }
});

This collection:

var Templates = Backbone.Collection.extend({
  model: app.Template,
  url: '/templates'
});

app.Templates = new Templates();

In my router:

this.view = new app.TemplateFormView({ model: new app.Template() });

And in that view:

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();
}

When I try to save the model, I get this error:

Uncaught Error: A "url" property or function must be specified

As you can see, the url is set in the collection and the model is a part of the collection. Any idea as to why the model doesn't seem to be a member of the collection?

Thanks!

Upvotes: 0

Views: 286

Answers (1)

Henrik Peinar
Henrik Peinar

Reputation: 2201

You might want to look into model.urlRoot parameter.

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.

Also check if your references are correct to app and app.Templates.

Upvotes: 1

Related Questions