我已经编写了一个自定义Backbone.sync方法,如下所示:
sync: function(method, model, options) {
switch(method)
case 'create':
//do stuff here
}
稍后,我使用set()设置所有内容,并通过调用save()调用同步。尽管我可以设置主干模型的属性,但在哪里可以将方法设置为“创建”,“删除”。等等?
最佳答案
您无需自己为method
指定Backbone.sync
参数。在以下情况下使用了不同的方法:create
-在新型号(没有id)上调用model.save()
时update
-在现有模型(具有ID)上调用model.save()
时patch
-致电model.save(..., {patch:true})
delete
-致电model.destroy()
read
-呼叫model.fetch()
或collection.fetch()