Javascript 将 Backbone 模型重置为初始默认值的最简单方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6889457/
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
Easiest way to reset Backbone's model to initial defaults?
提问by PhD
My models already have a defaults
hash. When parts of the view/page are reset, I wish to reset the models back to their original defaults.
我的模型已经有一个defaults
哈希值。当视图/页面的某些部分被重置时,我希望将模型重置回其原始默认值。
Currently, I explicitly set each attribute to its default value. Is there anything built in or a JavaScript/Underscore.js/Backbone.js/jQuery function that I could use to do this in a single statement?
目前,我明确地将每个属性设置为其默认值。是否有任何内置或 JavaScript/Underscore.js/Backbone.js/jQuery 函数可用于在单个语句中执行此操作?
回答by Peter Lyons
myModel.clear().set(myModel.defaults);
回答by saabeilin
I came up with the following approach:
我想出了以下方法:
reset: function () {
this.clear({silent: true});
this.set(this.defaults);
}
Having {silent: true}
in the clear()
call ensures that the change
event is not fired; it will be fire only on set()
call.
其{silent: true}
在clear()
呼叫确保change
事件不被解雇; 它只会set()
随叫随到。
回答by codeboy
I do this when the model has non-null initial object properties.
当模型具有非空初始对象属性时,我会这样做。
first, define defaultsas a function
首先,将默认值定义为函数
var MyModel = Backbone.Model.extends({
defaults:function(){
return {
AnArrayTypeProp:[]
};
}
});
second, when needed to reset model to default
第二,当需要将模型重置为默认值时
model.clear().set(model.defaults());
回答by Emile Bergeron
Based on saabeilin's answer and Backbone's annotated source, I came with an optimal function to fill the need for resetting a model.
根据saabeilin的回答和Backbone 的注释源,我提供了一个最佳函数来满足重置模型的需要。
Reusable reset
可重复使用的复位
/**
* Clears the model's attributes and sets the default attributes.
* @param {Object} attrs to overwrite defaults
* @param {Object} options to pass with the "set" call.
* @return {Backbone.Model} this object, to chain function calls.
*/
reset: function(attrs, options) {
// adds default option reset to true
options = _.extend({ reset: true }, options);
// ensure default params
var defaults = _.result(this, 'defaults');
attrs = _.defaults(_.extend({}, defaults, attrs), defaults);
// apply
this.clear({ silent: true }).set(attrs, options);
// triggers a custom event, namespaced to model in order
// to avoid collision with collection's native reset event
// when listening to a collection.
if (!options.silent) this.trigger('model:reset', this, options);
return this;
},
The following line ensures that even if an attribute is passed as undefined { test: undefined }
, it'll still have its default value.
以下行确保即使属性作为 undefined 传递{ test: undefined }
,它仍将具有其默认值。
attrs = _.defaults(_.extend({}, defaults, attrs), defaults);
Here's an example:
下面是一个例子:
var defaults = { test: 1 },
attrs = { test: undefined };
_.extend({}, defaults, attrs); // {test: undefined}
_.defaults(_.extend({}, defaults, attrs), defaults); // {test: 1}
Extending Backbone
扩展主干
And if you want it with all Backbone models, you can extend Backbone itself:
如果你想在所有 Backbone 模型中使用它,你可以扩展 Backbone 本身:
_.extend(Backbone.Model.prototype, {
reset: function(attributes, options){
/* ... */
}
});
Disclaimer: Don't do this if you're writing a JavaScript lib or Backbone plugin, as it could collide with another lib or it could cause a different behavior than the one expected by the person using your code.
免责声明:如果您正在编写 JavaScript 库或 Backbone 插件,请不要这样做,因为它可能与另一个库发生冲突,或者可能导致与使用您的代码的人预期的行为不同的行为。
回答by Johnny
I also thought about using model.clear()
and model.set()
in conjunction. Then I ran across the problem, that I trigger the change
event twice now.
Using the silent
option when calling model.clear()
is not an option, because I also want to have a change
event fired, when a property gets unset.
我也考虑过结合使用model.clear()
和model.set()
。然后我遇到了问题,我change
现在触发了两次事件。silent
在调用时使用该选项model.clear()
不是一个选项,因为我还希望change
在属性未设置时触发一个事件。
I ended up with adding a model.reset()
method. It takes a new attributes hash and fills this hash with undefined
values for old attributes keys not being present in the new attribute hash.
我最终添加了一个model.reset()
方法。它需要一个新的属性散列,并用新属性散列中undefined
不存在的旧属性键的值填充这个散列。
Model.prototype.reset = function(attrs, options) {
for (var key in this.attributes) {
if (key in attrs) continue;
attrs[key] = void 0;
}
return this.set(attrs, options);
};
This way you reset the models old attributes and get a valid change
event for every old and new attribute.
通过这种方式,您可以重置模型旧属性并change
为每个旧属性和新属性获取一个有效事件。
回答by ismnoiet
What about overriding the value of current model with a new empty model :
用一个新的空模型覆盖当前模型的值怎么样:
myModel = new model(); // default values will be considered
回答by Daniel Ortegón
My solutions is:
我的解决方案是:
model.clear({silent: true}).set(model.defaults);