javascript 强制 Backbone.sync 更新使用 POST 而不是 PUT 的最不丑陋的方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8527694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 03:44:00  来源:igfitidea点击:

What is the least ugly way to force Backbone.sync updates to use POST instead of PUT?

javascriptbackbone.js

提问by aw crud

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.emulateHTTPis not a perfect solution either.

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

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

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

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.

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

采纳答案by Paul

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
  }
}

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

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

回答by Andrés Torres Marroquín

For anyone who needs to force a POST/PUT request on the instance directly:

对于需要直接在实例上强制执行 POST/PUT 请求的任何人:

thing = new ModelThing({ id: 1 });
thing.save({}, { // options
    type: 'post' // or put, whatever you need
})

回答by Maanas Royy

Short and Sweet is put this on Top

Short and Sweet 放在最上面

Backbone.emulateHTTP = true;

This will use Get for Pull and Post for All pushes (read Create, Update, Delete)

这将使用 Get 进行所有推送的拉取和发布(阅读创建、更新、删除)

回答by Scott Evernden

the way I've done it is to override sync()thusly

我做的方式是重写sync()正是如此

Models.Thing = Backbone.Model.extend({
    initialize: function() {
        this.url = "/api/thing/" + this.id;
    },
    sync: function(method, model, options) {
        if (method === "read") method = "create";    // turns GET into POST
        return Backbone.sync(method, model, options);
    },
    defaults: {
        ...

回答by CodingWithSpike

I used a modification of Andres' answer and instead of havivng to remember to pass the option { type: 'post' }everywhere that I call .save()I instead just replaced the savefunction on the model to have it always add that option then call the base implementation. It felt cleaner...

我使用了对 Andres 答案的修改,而不是记住{ type: 'post' }在我调用的任何地方传递选项,.save()而是替换save模型上的函数,让它始终添加该选项,然后调用基本实现。感觉更干净了...

module.exports = Backbone.Model.extend({
  idAttribute: 'identifier',
  urlRoot: config.publicEndpoint,

  save: function (attributes, options) {
    // because the 'identifier' field is filled in by the user, Backbone thinks this model is never new. This causes it to always 'put' instead of 'post' on save.
    // overriding save here to always pass the option use post as the HTTP action.
    options = _.extend(options || {}, { type: 'post' });
    return Backbone.Model.prototype.save.call(this, attributes, options);
  }
});