仅使用 Backbone.js 更新某些模型属性

发布于 2024-10-21 16:42:43 字数 289 浏览 5 评论 0原文

使用 Backbone,我尝试仅更新一个属性并将其保存到服务器:

currentUser.save({hide_explorer_tutorial: 'true'});

但我不想发送所有其他属性。其中一些实际上是服务器端方法的输出,因此它们实际上并不是具有 setter 函数的真正属性。

目前,我正在使用 unset(attribute_name) 删除我不想在服务器上更新的所有属性。问题是这些属性将不再可供本地使用。

关于如何仅将某些属性保存到服务器的建议?

With Backbone, I'm trying to update and save to the server just one attribute:

currentUser.save({hide_explorer_tutorial: 'true'});

but I don't want to send all the other attributes. Some of them are actually the output of methods on the server-side and so they are not actually true attributes with setter functions.

Currently I'm using unset(attribute_name) to remove all the attributes that I don't want to update on the server. Problem is those attributes are then no longer available for local use.

Suggestions on how to only save certain attributes to the server?

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

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

发布评论

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

评论(5

梦纸 2024-10-28 16:42:43

从 Backbone 0.9.9 开始

只需将 {patch:true} 传递给 save 函数,如下所示

currentUser.save({hide_explorer_tutorial: 'true'}, {patch:true});

文档

如果您只想将更改后的属性发送到服务器,请调用 model.save(attrs, {patch: true})。您将收到一个仅包含传入属性的 HTTP PATCH 请求。

As of Backbone 0.9.9

Just pass {patch:true} to the save function, like this:

currentUser.save({hide_explorer_tutorial: 'true'}, {patch:true});

From the documentation,

If instead, you'd only like the changed attributes to be sent to the server, call model.save(attrs, {patch: true}). You'll get an HTTP PATCH request to the server with just the passed-in attributes.

往昔成烟 2024-10-28 16:42:43

您可以在模型上使用 toJSON 来执行此操作。

toJSON : function(){
  return {hide_explorer_tutorial: this.get("hide_explorer_tutorial")};
}

这将是保存时发送到后端的唯一属性。

You can use toJSON on the model to do so.

toJSON : function(){
  return {hide_explorer_tutorial: this.get("hide_explorer_tutorial")};
}

This will be the only attribute sent to the backend on save.

榆西 2024-10-28 16:42:43

事实上,有一种更简单的方法可以实现这一点,

如果您查看backbone.js第1145行,您会看到这

// Ensure that we have the appropriate request data.
    if (options.data == null && model && (method === 'create' || method === 'update' || method === 'patch')) {
      params.contentType = 'application/json';
      params.data = JSON.stringify(options.attrs || model.toJSON(options));
    }

意味着您可以通过将数据放入选项中来覆盖xhr的数据部分,

因为backbone保存需要model.save( [属性],[选项])

但请记住,像 id 这样的属性可能对于正确保存

示例

model.save( {}, { data: JSON.stringify(data) } ) ; 

对于您的特定情况

var data = { id : currentUser.id ,  hide_explorer_tutorial: 'true' }  ;  
currentUser.save( {}, { data : JSON.stringify(data) } );

这对我来说效果很好,并且可以与 xhr 的任何主干一起使用,例如获取,保存,删除, ...

感谢您的投票

In fact there is a much simpler way of achieving this

if you look at backbone.js line 1145 you will see that

// Ensure that we have the appropriate request data.
    if (options.data == null && model && (method === 'create' || method === 'update' || method === 'patch')) {
      params.contentType = 'application/json';
      params.data = JSON.stringify(options.attrs || model.toJSON(options));
    }

Which means that you may override the data part of the xhr by putting data in your options

Since backbone save requires model.save([attributes], [options])

But remember that attributes like id might be essential to proper saving

Example

model.save( {}, { data: JSON.stringify(data) } ) ; 

For your particular case

var data = { id : currentUser.id ,  hide_explorer_tutorial: 'true' }  ;  
currentUser.save( {}, { data : JSON.stringify(data) } );

This do the trick quite well for me and could be used with any backbone with xhr such as fetch, save, delete, ...

Thanks for voting

夜声 2024-10-28 16:42:43

我想这目前不可能:Backbone.js 部分模型更新

I guess this isn't currently possible: Backbone.js partial model update

别理我 2024-10-28 16:42:43

有一个技巧,如果您没有在 options 参数上设置 data 属性,而是设置 attrs 属性,则请求负载将是 attrs 属性值而不是所有模型属性。

注意:这也适用于模型创建操作 (POST)。

对于您的特定情况:

var data = { hide_explorer_tutorial: 'true' };  
currentUser.save(data, { attrs : data });

您可以从backbone.js 1.33 找到更多详细信息源代码(第1405 - 1409行):

// Ensure that we have the appropriate request data.
if (options.data == null && model && (method === 'create' || method === 'update' || method === 'patch')) 
{
  params.contentType = 'application/json';
  params.data = JSON.stringify(options.attrs || model.toJSON(options));
}

There is a trick, if you do not set data property but attrs property on the options argument, the request payload would be the attrs property value instead of all the model attributes.

Note: This also works for model create actions (POST).

For your particular case:

var data = { hide_explorer_tutorial: 'true' };  
currentUser.save(data, { attrs : data });

You can find more details from the backbone.js 1.33 source code (line 1405 - 1409):

// Ensure that we have the appropriate request data.
if (options.data == null && model && (method === 'create' || method === 'update' || method === 'patch')) 
{
  params.contentType = 'application/json';
  params.data = JSON.stringify(options.attrs || model.toJSON(options));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文