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

发布于 2024-12-21 06:19:56 字数 313 浏览 0 评论 0原文

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

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

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

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.

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

倾听心声的旋律 2024-12-28 06:19:56

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

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

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

thing = new ModelThing({ id: 1 });
thing.save({}, { // options
    type: 'post' // or put, whatever you need
})
世界如花海般美丽 2024-12-28 06:19:56

Short and Sweet 将此放在顶部

Backbone.emulateHTTP = true;

这将使用 Get 进行 Pull 并使用 Post 进行所有推送(请阅读创建、更新、删除)

Short and Sweet is put this on Top

Backbone.emulateHTTP = true;

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

ゞ花落谁相伴 2024-12-28 06:19:56

将同步(方法,模型,[选项])直接添加到您需要覆盖的模型中。

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

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

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

简单 2024-12-28 06:19:56

我这样做的方法是重写 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: {
        ...

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

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: {
        ...
烟柳画桥 2024-12-28 06:19:56

我使用了 Andres 答案的修改,而不是记住在我调用 .save() 的任何地方传递选项 { type: 'post' } 我只是替换了模型上的 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);
  }
});

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 save function on the model to have it always add that option then call the base implementation. It felt cleaner...

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);
  }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文