YUI 3.x Home -

Site Search (YDN & YUIBlog):

# YUI Library Examples: Attribute: readOnly and writeOnce Configuration

Note: This is YUI 3.x. Looking for YUI 2.x?

## Attribute: readOnly and writeOnce Configuration

Attributes can be configured to be `readOnly`, stopping them from being modified by the end user, or `writeOnce` allowing them to only be set by the end user, but only once. This example demonstrates how to setup attributes for your class as `readOnly` or `writeOnce` attributes, and shows how their behavior differs when the end user attempts to set their values.

View example in new window.

### ReadOnly And WriteOnce

Attribute supports the ability to configure attributes to be `readOnly` or `writeOnce`. `readOnly` attributes cannot be set by the end user, either through initial values passed to the constructor, or by invoking the `set` method. `writeOnce` attributes on the other hand, can be set by the user, but only once, either during initialization or through a call to `set`. Once a value is established for a `writeOnce` attribute, it cannot be reset to another value by the user.

### Configuring ReadOnly And WriteOnce Attributes

This example sets up a custom class, `MyClass`, with two attributes, `foo` and `bar`. `foo` is configured to be a `readOnly` attribute, and `bar` is configured to be a `writeOnce` attribute:

// Setup a custom class with attribute support function MyClass(cfg) { this.addAttrs(Y.merge(MyClass.ATTRIBUTES), cfg); } // Setup attribute configuration MyClass.ATTRIBUTES = { "foo" : { value: "Default Foo", readOnly: true }, "bar" : { value: "Default Bar", writeOnce: true } };

### Attempting To Set Values

We first attempt to set values for both attributes in the constructor (used to intialize the attributes) and see that only `bar`, the `writeOnce` attribute, gets set to the user provided value:

var o1 = new MyClass({ foo: "User Foo", bar: "User Bar" }); print("Initial Set", "header"); print("foo value (readOnly):", "subheader"); print(o1.get("foo")); print("bar value (writeOnce):", "subheader"); print(o1.get("bar"));

We then attempt to set values for both attributes again, using `set`, and see that niether of the values are modified:

print("Try Setting Again", "header"); o1.set("foo", "User Reset Foo"); o1.set("bar", "User Reset Bar"); print("Final foo value (readOnly)", "subheader"); print(o1.get("foo")); print("Final bar value (writeOnce)", "subheader"); print(o1.get("bar"));

### Setting The State Of ReadOnly Values Internally

Although the user cannot update the value of `readOnly` attributes, it maybe neccessary for the host object to update it's value internally. The example shows how this can be done, using the private `_set` property on the host:

MyClass.prototype.doSomething = function() { // ... Do something which requires // MyClass to change the value // of foo ... // Host code can reset value of // readOnly attributes interally, // by working with the private _set property this._set("foo", "New Default Foo"); };

Copyright © 2009 Yahoo! Inc. All rights reserved.

Privacy Policy - Terms of Service - Copyright Policy - Job Openings