Create Custom Element Property Getters and Setters with MooTools

By  on  

One of the goals of the MooTools projects is to create very flexible methods by which developers may modify or extend the behavior of given classes. One example of that flexibility is the ability to customize the Element class' get and set methods for specified values.

The default behavior of the get and set methods for an object are to return/modify the given Element attribute. Take the following for example:

var alt = document.id('myImage').get('title');  //returns the image's title attribute text

The above code modifies the title attribute for the given element. Simple, of course, but then MooTools also uses the get/set methods to set and retrieve items that aren't element attributes:

var tag = document.id('myImage').get('tag'); //returns images's tag (img)

The above code retrieves the given element's tag name. How does MooTools do this? Simple: the Element.Properties object. You can set custom get and set handling by adding your desired string to the Element.Properties object. Here's an example:

Element.Properties.yourCustomGetOrSetName = {
	get: function() {
		//your custom programming to accomplish what should be returned...
		return yourReturnValue;
	},
	set: function(input) {
		//your custom programming to accomplish what should be set...
		//don't need to return anything
	},
	erase: function() { //erase works too!
		//programming to erase this
	}
};

Here's the code MooTools uses internally to accomplish the get('tag') ability:

Element.Properties.tag = {
	get: function(){
		return this.tagName.toLowerCase();
	}
};

So how could you use this? You could use Element.Properties to create a parent getter:

Element.Properties.parent = {
	get: function() {
		return this.getParent(); //returns the element's parent
	}
	//no need to do a setter or eraser
};
//.....
//usage
var parent = document.id('myElement').get('parent'); //returns myElement's parent element.

Pretty sweet, no? Have any other items you'd like to see in the get or set methods? Share!

Recent Features

  • By
    Regular Expressions for the Rest of Us

    Sooner or later you'll run across a regular expression. With their cryptic syntax, confusing documentation and massive learning curve, most developers settle for copying and pasting them from StackOverflow and hoping they work. But what if you could decode regular expressions and harness their power? In...

  • By
    LightFace:  Facebook Lightbox for MooTools

    One of the web components I've always loved has been Facebook's modal dialog.  This "lightbox" isn't like others:  no dark overlay, no obnoxious animating to size, and it doesn't try to do "too much."  With Facebook's dialog in mind, I've created LightFace:  a Facebook lightbox...

Incredible Demos

  • By
    MooTools Fun with Fx.Shake

    Adding movement to your website is a great way to attract attention to specific elements that you want users to notice. Of course you could use Flash or an animated GIF to achieve the movement effect but graphics can be difficult to maintain. Enter...

  • By
    MooTools Gone Wild: Element Flashing

    If you're like me and lay awake in bed at night, you've flipped on the TV and seen the commercials: misguided, attention-starved college girls fueled by alcohol ruining their futures by flashing lame camera-men on Spring Break. Why do they do it? Attention...

Discussion

  1. I was asking yesterday on the mootools irc channel if there is a way to add a property to css properties so I can do something similar, tweening -webkit-box-shadow and -moz-box-shadow, only using one property, like this:

    el.tween('box-shadow', xxx);

    Do you know if this is possible?

  2. Bill

    How do I get just the parent id, not the parent?

  3. @Bill: Like this:

    var parent = document.id('myElement').get('parent').get('id');

  4. I know this article was from a while ago, but I’ve just discovered it and it is relevant to something I have been attempting recently.

    You asked if there are any other items we would like in the properties set and my suggestion is this:

    Element.Properties.children = {
        get: function() { 
            return this.getChildren();
        },
        set: function(value) {
            this.adopt(value);
        }
    };
    

    which would then allow you to instantiate more complex Element hierarchies as follows:

    new Element('div.background', { children: [
        new Element('div.container', { children: [
            new Element('div.window', { children: [ 
                new Element('h1', { text: 'A title' }),
                new Element('div.content'),
                new Element('div.bar', { children: [
                    new Element('button', { text: 'OK', events: { ... }}),
                    new Element('button', { text: 'Cancel', events: { ... }})
                ]})
            ]})
        ]})
    ]});
    

    With this you can easily see (and modify) the hierarchy instead of having to follow through all of the inject/adopt/grab calls.

    Regards,
    Dan

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!