同步时排除模特属性(Backbone.js的) [英] Exclude model properties when syncing (Backbone.js)
问题描述
有没有办法从我的模型排除某些属性时,我同步?
Is there a way to exclude certain property from my model when I sync?
例如,我请一些视图状态我的模型信息。比方说,我有一个选择器模块,并且该模块只需拨动一个选
我的模型属性。后来,当我打电话 .save()
我的收藏,我愿意忽略的选择的值
和从同步到服务器中排除。
For example, I keep in my model information about some view state. Let's say I have a picker module and this module just toggle a selected
attributes on my model. Later, when I call .save()
on my collection, I'd want to ignore the value of selected
and exclude it from the sync to the server.
是否有这样做的一个干净的方式?
Is there a clean way of doing so?
(让我知道,如果你想了解更多详细信息的)
推荐答案
这似乎是最好的解决方案(基于@nikoshr引用的问题)
This seems like the best solution (based on @nikoshr referenced question)
Backbone.Model.extend({
// Overwrite save function
save: function(attrs, options) {
options || (options = {});
attrs || (attrs = _.clone(this.attributes));
// Filter the data to send to the server
delete attrs.selected;
delete attrs.dontSync;
options.data = JSON.stringify(attrs);
// Proxy the call to the original save function
return Backbone.Model.prototype.save.call(this, attrs, options);
}
});
所以,我们覆盖节省模型实例功能,但我们只是过滤掉我们不需要的数据,然后我们代理的父原型的功能。
So we overwrite save function on the model instance, but we just filter out the data we don't need, and then we proxy that to the parent prototype function.
这篇关于同步时排除模特属性(Backbone.js的)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!