Domanda

I have a model in 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"});. If I use m.reset() , the attributes are set to "a" and "b". What is the most convenient way to reset them to the default values?

È stato utile?

Soluzione

reset() resets the value to its initial value, which is the value you supplied to the constructor. 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. If you'll want to do this on many different model classes, the class extension is probably the way to go

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top