2016-03-07 35 views
0

我正在学习BackboneJS。在下面的代码this.at(0).destroy();:用REST后端我想发出一个HTTP一致DELETEBackboneJS:REST DELETE不会触发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')); 
}); 

该模型是从集合中删除,但没有休息DELETE发生到后端。 REST后端的删除与'localhost:8080/todos/0'一起使用。 请告诉我缺少的东西。

+2

您的模型有ID吗? –

回答

0

对REST后端的删除工作与“本地主机:8080 /待办事项/ 0”

你可能认为主干发出基于收集,模型的指数,这是不是这样的请求。附加到收集网址的参数是该模型的idid是主干用于检查模型是否被保留的。

您的模型可能没有id属性或idAttribute集合,因此backbone认为模型尚未保存在持久层中,因此不需要发出DELETE请求。

+0

谢谢!现在我了解到,在模型中没有ID,骨干不考虑持久性,只能乐观地从集合中删除它。 – Tudor