强制 Backbone.sync 更新使用 POST 而不是 PUT 的最不丑陋的方法是什么? [英] What is the least ugly way to force Backbone.sync updates to use POST instead of PUT?

查看:24
本文介绍了强制 Backbone.sync 更新使用 POST 而不是 PUT 的最不丑陋的方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的一些 Backbone 模型应该始终使用 POST,而不是 POST 用于创建和 PUT 用于更新.我保留这些模型的服务器能够支持所有其他动词,因此使用 Backbone.emulateHTTP 也不是一个完美的解决方案.

Some of my Backbone models should always use POST, instead of POST for create and PUT for update. The server I persist these models to is capable of supporting all other verbs, so using Backbone.emulateHTTP is not a perfect solution either.

目前我覆盖了这些模型的 isNew 方法并让它返回 true,但这并不理想.

Currently I override the isNew method for these models and have it return true, but this is not ideal.

除了直接修改backbone.js代码之外,有没有一种简单的方法可以逐个模型地实现这个目标?我的一些模型可以使用 PUT(它们被持久化到支持所有动词的不同服务器,包括 PUT),因此将 Backbone.sync 替换为将更新"方法转换为创建"的方法也不理想.

Other than modifying the backbone.js code directly, is there a simple way to achieve this goal on a model-by-model basis? Some of my models can use PUT (they are persisted to a different server that supports all verbs, including PUT), so replacing Backbone.sync with one that converts the 'update' method to 'create' is not ideal either.

推荐答案

直接向您需要覆盖的模型添加同步(方法、模型、[选项]).

add a sync(method, model, [options]) directly to your models you need to override.

YourModel = Backbone.Model.extend({
  sync: function(method, model, options) {
    //perform the ajax call stuff
  }
}

以下是更多信息:http://documentcloud.github.com/backbone/#Sync

这篇关于强制 Backbone.sync 更新使用 POST 而不是 PUT 的最不丑陋的方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆