简体   繁体   English

在YUI3模型中,有没有一种方法可以重置为默认值?

[英]In YUI3 Model, is there a way to reset to default value?

I have a model in YUI3: 我在YUI3中有一个模型:

var AModel = Y.Base.create("aModel", Y.Model, [], {
}, {
    ATTRS: {
        'att1': {
            value: "DEFAULT1"
        },
        'att2': {
            value: "DEFAULT2"
        },
    }
});

The models are created using var m = AModel({att1: "a", att2: "b"}); 使用var m = AModel({att1: "a", att2: "b"});创建模型var m = AModel({att1: "a", att2: "b"}); . If I use m.reset() , the attributes are set to "a" and "b" . 如果使用m.reset() ,则属性设置为"a""b" What is the most convenient way to reset them to the default values? 将它们重置为默认值的最便捷方法是什么?

reset() resets the value to its initial value, which is the value you supplied to the constructor. reset()将值重置为其初始值,该初始值是您提供给构造函数的值。 You can either explicitly set the values back: 您可以将值显式设置回:

m.set("att1",AModel.ATTRS.att1.default);
m.set("att2",AModel.ATTRS.att2.default);

or construct the model with no attributes, then set the values: 或构建没有属性的模型,然后设置值:

var m = AModel();
m.setAttrs({att1: "a", att2: "b"});

You could also write a Model extension class which provides a setAttrsToDefault() method which could iterate over the model's ATTRS, setting the attributes back to the default value. 您还可以编写一个Model扩展类,该类提供一个setAttrsToDefault()方法,该方法可以遍历模型的ATTRS,并将属性设置回默认值。 If you'll want to do this on many different model classes, the class extension is probably the way to go 如果要在许多不同的模型类上执行此操作,则可以使用类扩展

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM