2013-03-22 55 views
0

我无法弄清楚我做错了什么。我的成功和错误回调不会触发,我从服务器返回JSON响应。任何想法,我在哪里搞砸了?Backbone销毁功能的成功回调不会触发

mysite.city.delete_photo = { 
    "Model": Backbone.Model.extend({ 
     "id": "deletePhoto", 
     "initialize": function initialize(id) { 
      this.url = "/assets/ajax/delete_image.php?img_id=" + id; 
     } 
    }), 
    "View": Backbone.View.extend({ 
     "initialize": function initialize(id) { 
      _.bindAll(this, 
       "render", 
       "success"); 

      this.model = new mysite.city.delete_photo.Model(id); 

      this.render(); 
     }, 
     "render": function render() { 
      this.model.destroy({ 
       "cache": false, 
       "success": this.success, 
       "error": this.error 
      }); 
     }, 
     "success": function success(model, data) { 
      console.log("test"); 
     }, 
     "error": function error() { 
      message('error', 'This image could not be deleted.'); 
     } 
    }) 
}; 
+2

缩进。绝对缩进。 :) – sje397 2013-03-22 13:34:18

+0

我的编码风格的作品。这不是缩小它的缩进。 – 2013-03-22 13:38:27

+0

我在开玩笑。我对文档的阅读没有提供View类的成功和错误属性。也许你可以在模型中添加'sync'属性,并将错误和成功处理程序作为选项传递给它? – sje397 2013-03-22 13:47:25

回答

0

successerror不是型号的功能,但两者都对模型的提取和保存方法的回调。与error回调

model.fetch({success: function(model, response, options) { 
    console.log('success triggered'); 
}); 

相同的逻辑:以打击代码获取时,你可以解雇成功或错误事件。

+0

我不同意。你应该能够在销毁函数中有成功和错误回调http://documentcloud.github.com/backbone/#Model-destroy – 2013-03-22 13:58:28

+0

你是对的,模型中的很多方法都有成功和错误回调,例如摧毁方法。它是一样的逻辑如何销毁方法来引发成功或错误,如model.fetch。 – Shuping 2013-03-22 14:03:21

0

行之后

this.model = new mysite.city.delete_photo.Model(id); 

应该是:

this.model = new mysite.city.delete_photo.Model({'id':id}); 

在第一种情况下Id没有设置在所有的模型和与destroy返回false,甚至没有发出一个DELETE请求服务器,因为该型号没有ID并且被认为是new

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