This example builds on the "Basic Configuration" example, showing how you can use attribute to model objects in your application.

As with the basic example, it is geared towards users who want to create their own classes from scratch and add attribute support. In most cases you should consider extending the `Base` class when you need attribute support, instead of augmenting Attribute directly. `Base` does the work described in this example for you, in addition to making it easier for users to extend you class.

{{>attribute-basic-speeddate-source}}

Setting Up a SpeedDater Class

In this example, we'll create a custom `SpeedDater` class, and show how you can use attributes to manage the state for a Speed Dater. In the "Attribute Event Based Speed Dating" example we'll modify this example to leverage attribute change events.

Creating A YUI Instance

As with the other attribute examples, we'll setup our own instance of the YUI object and request the modules we require using the code pattern shown below:

``` ```

Defining The SpeedDater Class

The first step in the example is to create the constructor function for our new class, to which we want to add attribute support. In our example, this class is called `SpeedDater`. We then augment `SpeedDater` with `Y.Attribute`, so that it receives all of `Attribute's` methods, in addition to any it may defined itself:

``` // Setup custom class which we want to add attribute support to function SpeedDater(cfg) { ... } // Augment custom class with Attribute Y.augment(SpeedDater, Y.Attribute); ```

Adding Attributes

We can now set up any attributes we need for `SpeedDater` using Attribute's `addAttrs()` method. For this example we add 3 attributes - `name`, `personality`, and `available`. We provide an default initial `value` for `personality` and `available`, but don't have anything for `name`. As mentioned in the basic example, the same object literal we use to provide the initial value for the attribute can also be used to configure attribute properties such as `readOnly` or `writeOnce`, and to define `getter`, `setter` and `validator` methods for the attribute. For `name`, we configure it to be `writeOnce`, meaning that it's value can be set once by the user, but not modified after that single set.

The default set of attributes which `SpeedDater` will support is passed to `addAttrs` to set up the attributes for each instance during construction.

As mentioned previously, if you expect your class to be extended, Base provides a more convenient way for you to define the same attribute configuration statically for your class, so that it can be modified by extended classes. Base will take care of isolating the static configuration, so that it isn't modified across instances.

The complete definition for `SpeedDater` is shown below:

``` // Setup custom class which we want to // add managed attribute support to function SpeedDater(cfg) { // When constructed, setup the initial attributes for the // instance, by calling the addAttrs method. var attrs = { // Add 3 attributes: name, personality, available name : { writeOnce:true }, personality : { value:50 }, available : { value:true } }; this.addAttrs(attrs, cfg); } SpeedDater.prototype.applyNameTag = function(where) { // Method used to render the visual representation of a // SpeedDater object's state (in this case as a name tag) }; SpeedDater.prototype.updateNameTag = function() { // Method used to update the rendered state of SpeedDater in the DOM. } // Template to use form the markup SpeedDater.NAMETAG = "
Hello!
...
"; // Augment custom class with Attribute Y.augment(SpeedDater, Y.Attribute); ```

The `addAttrs()` method, in addition to the default attribute configuration, also accepts an object literal (associative array) of name/value pairs which can be used to over-ride the default initial values of the attributes. This is useful for classes which wish to allow the user the set the value of attributes as part of object construction, as shown by the use of the `cfg` argument above.

Using Attributes

Now that we have `SpeedDater` defined with the set of attributes it supports, we can create multiple instances of `SpeedDater` defining the initial attribute state for each instance through the constructor. We can also update the instance's attribute state after construction, using the `get` and `set` methods defined by Attribute.

We create a first instance, `john`, setting up the intial state using Attribute's constructor configuration object support:

``` // Set both name and personality during construction john = new SpeedDater({ name: "John", personality: 76.43 }); ```

For the second instance that we create, `jane`, we set the value of the personality attribute, after construction:

``` // Set name during construction jane = new SpeedDater({ name: "Jane" }); // Set personality after construction. The initial value for personality // in this case, will be the value defined when the attribute was added // using addAttrs (above) jane.set("personality", 82); ```

We render the current attribute state of each instance to the DOM, using the `applyNameTag()` method defined on SpeedDater's prototype:

``` // Render the sticker with john's state information to the DOM john.applyNameTag("#john .shirt"); // Render the sticker with jane's state information to the DOM jane.applySicker("#jane .shirt"); ```

Although not directly related to working with Attributes, it's worth taking a look at the `applyNameTag()` and `updateNameTag()` implementations, since they establish a commonly used pattern.

The `applyNameTag()` method handles rendering the initial visual representation for a speed dater object's state (in this case a name tag). It uses tokens in an HTML "template" string, which it replaces with the values of attributes using the `Y.Lang.sub()` utility method:

``` // A template for the markup representing the SpeedDater object.. SpeedDater.NAMETAG = '
\
Hello!
\
I\'m {name} \ and my PersonalityQuotientIndex is \ {personality} \
\
{available}
\
'; ``` ``` // A rendering method, used to create the initial markup for the SpeedDater. SpeedDater.prototype.applyNameTag = function(where) { // This example uses an HTML template string containing placeholder // tokens (SpeedDater.NAMETAG above), and Y.Lang.sub() to replace the // tokens with the current attribute values. var tokens = { // Get attribute values and map them to the tokens in the HTML template string name: this.get("name"), available: (this.get("available")) ? "I'm still looking " : "Sorry, I'm taken", personality: this.get("personality") }; // Create a new Node element, from the token substituted string... this.nameTag = Y.Node.create(Y.Lang.sub(SpeedDater.NAMETAG, tokens)); // ... and append it to the DOM Y.one(where).appendChild(this.nameTag); }; ```

The `updateNameTag()` method handles updating this visual representation with the current state, when requested by the user

``` // An update method, used to refresh the rendered content, after // an attribute value is changed. SpeedDater.prototype.updateNameTag = function() { // Get current attribute values... var taken = (this.get("available")) ? "I'm still looking " : "Sorry, I'm taken"; var name = this.get("name"); var personality = this.get("personality"); // Find the corresponding element, and replace the innerHTML with the new value this.nameTag.one(".sd-name").set("innerHTML", name); this.nameTag.one(".sd-availability").set("innerHTML", taken); var personalityEl = this.nameTag.one(".sd-personality"); personalityEl.set("innerHTML", personality); if (personality > 90) { personalityEl.addClass("sd-max"); } } ```

Each instance's state can be now be updated using Attribute's `set` method, and the subsequent call to SpeedDater's `updateNameTag()` method will update the visual representation (the rendered DOM) of the object:

``` Y.on("click", function() { john.set("available", false); john.updateNameTag(); }, "#john .taken"); Y.on("click", function() { jane.set("available", false); jane.updateNameTag(); }, "#jane .taken"); Y.on("click", function() { jane.set("personality", 98); jane.updateNameTag(); }, "#jane .upgrade"); ```

In the "Attribute Event Based Speed Dating" example, we'll see how we can use Attribute change events to eliminate the need for users to call `updateNameTag()` each time they set an attribute, and have the two instances communicate with one another.

Complete Example Source

``` {{>attribute-basic-speeddate-source}} ```