BackboneJS:REST DELETE不会在destroy()上触发

时间:2016-03-07 19:23:03

标签: rest backbone.js

我正在学习BackboneJS。使用REST后端,我尝试在下面的代码中发出一行HTTP DELETE:this.at(0).destroy();

var Task = Backbone.Model.extend({
  defaults: {
    name: 'Testing Just',
  },
  url: 'http://localhost:8080/todos/',
});

var Tasks = Backbone.Collection.extend({
  model: Task,
  url: 'http://localhost:8080/todos/'
});

var tasks = new Tasks();
tasks.fetch({
  context: tasks
}).done(function() {
  console.log("Tasks:" + this.length)
  console.log(this.at(0).get('name'));
  this.at(0).destroy();
  console.log("Tasks:" + this.length);
  console.log(this.at(0).get('name'));
});

从集合中删除模型,但后端不会发生REST DELETE。 REST后端的删除与'localhost:8080 / todos / 0'一起使用。 请告知我缺少的东西。

1 个答案:

答案 0 :(得分:0)

  

REST后端的删除适用于'localhost:8080 / todos / 0'

您可能认为骨干基于集合中的模型索引发出请求,但实际情况并非如此。附加到集合的URL的参数是模型的idid是骨干用于检查模型是否持久化的内容。

您的模型可能没有id属性或idAttribute集,因此,骨干认为模型尚未保存在持久层中,并且无需发出DELETE请求。< / p>

相关问题