backbone destroy不发送DELETE HTTP

时间:2014-06-19 14:36:24

标签: javascript backbone.js model destroy http-delete

当创建我的模型时,我没有身份证明这是我的功能破坏的原因,你能不能帮助我

我有另一个id,是clientId:

保存模型的功能:

 save : function (){      
              var result =this.model.save({
                    ClientId: this.$('.clientId-field').val(),
                   companyName: this.$('.companyName-field').val()
                   } ,
   delete: function() {

   this.model.destroy();
    return false;
}

我的对象:

0: {ClientId:1, companyName:nom compagnie,…}
   companyName: "nom compagnie"

请帮帮我

child {collection: child, _deferProcessing: false, _listenerId: "l4", _events: Object, _queue: Backbone.BlockingQueue…}
_attributeChangeFired: false
_changing: false
_deferProcessing: false
_events: Object
_isInitialized: true
_listenerId: "l4"
_pending: false
_permitsUsed: 0
_previousAttributes: Object
_queue: Backbone.BlockingQueue
_relations: Object
attributes: Object
changed: Object
cid: "c5"
collection: child

2 个答案:

答案 0 :(得分:1)

如果您希望使用与id不同的唯一密钥。然后你可以设置一个Model idAttribute来透明地从那个键映射到id。

http://backbonejs.org/#Model-idAttribute

var MyModel = Backbone.Model.extend({

   urlRoot : "/data/delete",   //root delete url 

   idAttribute: "clientId"
});

var myModel = new MyModel({
   clientId : "123"
});

myModel.destroy();

destroy方法调用后,DELETE请求将发送到网址DELETE /data/delete/123

答案 1 :(得分:0)

当我这样做时,我的函数this.model.save发送一个PUT HTTP而不是POST HTTP

   var ClientModel = Backbone.RelationalModel.extend({
    idAttribute: "ClientId",
    validation : {
         companyName : {
               required: true,
               msg: 'companyName is required.'
}
 });
  return ClientModel;

 });

你必须知道,我在调用我的函数this.model.save

之前生成了我的id
save : function (){ 
           var clientId= this.random ();
          var result =this.model.save({
                ClientId: clientId,
               companyName: this.$('.companyName-field').val()
               } ,
 delete: function() {

 this.model.destroy();
   return false;
    }