milo

Attribute

function
 Attribute() 

Option name Type Description
el Element

DOM element where attribute is attached

name String

Optional name of the attribute, usually supplied by subclass via attrName method

An absctract class for parsing and validation of element attributes.
Subclasses should define methods attrName, parse, validate and render.

function Attribute(el, name) {
    this.name = name || this.attrName();
    this.el = el;

    // attribute node
    this.node = el.attributes[this.name];
}


_.extend(Attribute, {
    remove: Attribute$$remove
});

####Attribute instance methods####

The following instance methods should be defined by subclass

  • attrName - should return attribute name
  • parse - should parse attribute value
  • validate - should validate attribute value, throwing exception if it is incorrect
  • render - should return attribute value for a given attribute state (other properties, as defined in subclass)
_.extendProto(Attribute, {
    get: Attribute$get,
    set: Attribute$set,
    remove: Attribute$remove,
    decorate: Attribute$decorate,

    destroy: Attribute$destroy,

    // should be defined in subclass
    attrName: toBeImplemented,
    parse: toBeImplemented,
    validate: toBeImplemented,
    render: toBeImplemented
});


function Attribute$$remove(el, deep) {
    var name = this.prototype.attrName();
    el.removeAttribute(name);

    if (deep) {
        var selector = '[' + name + ']';
        var children = el.querySelectorAll(selector);
        _.forEach(children, function(childEl) {
            childEl.removeAttribute(name);
        });
    }
}


function Attribute$remove() {
    delete this.node;
}


function Attribute$destroy() {
    delete this.el;
    delete this.node;
}

Attribute$get

function
 Attribute$get() 

Attribute instance method that returns attribute value as string.

function Attribute$get() {
    return this.el.getAttribute(this.name);
}

Attribute$set

function
 Attribute$set() 

Option name Type Description
value String

Attribute instance method that sets attribute value.

function Attribute$set(value) {
    this.el.setAttribute(this.name, value);
}

Attribute$decorate

function
 Attribute$decorate() 

Attribute instance method that decorates element with its rendered value.
Uses render method that should be defiend in subclass.

function Attribute$decorate() {
    this.set(this.render());
}


function toBeImplemented() {
    throw new Error('calling the method of an absctract class');
}