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

查看:113
本文介绍了迫使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替换为将'update'方法转换为'create'的服务器也不理想。

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

Here's some more information: http://documentcloud.github.com/backbone/#Sync

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

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