admin管理员组文章数量:1186136
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?
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?
Share Improve this question edited Oct 7, 2013 at 10:28 Nikhil Agrawal 26.5k21 gold badges92 silver badges127 bronze badges asked Mar 15, 2011 at 0:06 Drew Dara-AbramsDrew Dara-Abrams 8,05411 gold badges42 silver badges48 bronze badges5 Answers
Reset to default 20As 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.
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.
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
I guess this isn't currently possible: Backbone.js partial model update
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));
}
本文标签: javascriptonly update certain model attributes using BackbonejsStack Overflow
版权声明:本文标题:javascript - only update certain model attributes using Backbone.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738337495a2077190.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论