admin管理员组

文章数量:1317906

I have a very basic backbone (sample) application which just creates and destroys model items. When the model is created the object is persisted with a POST to the web server, but when the model is destroyed there is no DELETE sent to the server? Any idea why this might be?

very basic model:

window.User = Backbone.Model.extend({
  urlRoot: 'users'
});

my test code just to create and delete the model:

var model = null;

$(".add").click(function(){
  if (model == null) {
    model = new window.User;
    model.set({name: 'meeee'});
    model.save();
  }
});

$(".remove").click(function(){
  if (model != null) {
    model.destroy();
  }
});

The JSON response when creating the model seems good too:

I have a very basic backbone (sample) application which just creates and destroys model items. When the model is created the object is persisted with a POST to the web server, but when the model is destroyed there is no DELETE sent to the server? Any idea why this might be?

very basic model:

window.User = Backbone.Model.extend({
  urlRoot: 'users'
});

my test code just to create and delete the model:

var model = null;

$(".add").click(function(){
  if (model == null) {
    model = new window.User;
    model.set({name: 'meeee'});
    model.save();
  }
});

$(".remove").click(function(){
  if (model != null) {
    model.destroy();
  }
});

The JSON response when creating the model seems good too:

Share Improve this question edited Jun 3, 2012 at 22:26 Nippysaurus asked Jun 3, 2012 at 21:50 NippysaurusNippysaurus 20.4k21 gold badges80 silver badges130 bronze badges 2
  • 3 how does your server respond to the save request? If the model doesn't have an .id attribute (Which the server is supposed to return when saving), it won't send any request. – Esailija Commented Jun 3, 2012 at 21:53
  • updated the question with screenshot of json response. – Nippysaurus Commented Jun 3, 2012 at 22:27
Add a ment  | 

1 Answer 1

Reset to default 12

Backbone doesn't know that _id is the name you are using for your id field, unless you've modified your copy of Backbone.js somehow (please don't). To tell Backbone what the name of your id field is for some particular model, idAttribute in your Model definition. Otherwise, it defaults to the regular id.

Without the "identity" field set, Backbone is going to consider that the the model instance "isNew" (a model instance which hasn't been persisted to the server), so when there's no "identity" (as in your case, as it doesn't know that your identity is, instead, _id) there wouldn't be any need to destroy it on the server.

本文标签: javascriptwhy does backbone not send the deleteStack Overflow