June 11th, 2008
5 Comments

MooTools 1.1 To 1.2 - Classes

My favourite JavaScript framework MooTools just got released as version 1.2, with tons of new features, performance boosts and bugfixes. To make the API consistent and bullet-proof for the future, the new release introduces several changed methodnames and arguments that break 1.11 and 1.2 beta code.

So I start a series about understanding the changes and converting to MooTools 1.2, starting with the powerful OOP helper Class.

Creating and Extending Classes

Classes allow to inherit and extend methods and properties from other Classes.

Class in 1.11

var Person = new Class({

    initialize: function(name) {
        this.name = name;
    }

});

var Buyer = Person.extend({

    options: {
        status: 0,
        url: null
    },

    initialize: function(name, options) {
        this.parent(name); // extends Person, so we call the parent method
        this.setOptions(options); // method from the class Options
    }

});

Buyer.implement(Options);

Class in 1.2

Class::extend and implement are now called inside the new Class:

var Person = new Class({

    initialize: function(name) {
        this.name = name;
    }

});

var Buyer = new Class({

    Extends: Person,

    Implements: Options, // one or more (as Array) classes to implement

    options: {
        status: 0,
        url: null
    },

    initialize: function(name, options) {
        this.parent(name); // extends Person, so we call the parent method
        this.setOptions(options); // method from the class Options
    }

});

Here you see the 2 mutators Extends and Implements. The Class creation is extendable, you can even add your own logic.

Extending the Mutators

The following code adds a Class property Bounds, to set the scope of a method exclusively to the current class (usually for Eventhandler, where you would need Function::bind).

    Class.Mutators.Bounds = function(self, methods) {

        $splat(methods).each(function(method){
            var fn = self[method];
            self[method] = function() {
                return fn.apply(self, arguments);
            };
        });

    };

The following example class uses the Bounds property to bind 2 methods to the class:

var Toggler = new Class({

    Bounds: ['mouseleave', 'mouseenter'],

    initialize: function(element) {
        this.element = $(element);

        // no .bind(this) needed for the eventhandler
        // without Bounds the scope would be the element
        this.element.addEvents({
            'mouseenter': this.mouseenter, 
            'mouseleave': this.mouseleave
        });
    },

    mouseenter: function() {
        // "this" is always your current class
        this.somthingCool();
    },

    mouseleave: function() {
        // "this" is always your current class, too
        this.somthingAmazing();
    },

    // ... more methods

});

Any ideas for more mutators? What would you like to know about MooTools 1.2. What do you like and what would you make better?

To be continued …

Keywords:
Share it: Stumble it!Digg This!del.icio.us (No Posts)

discussion by DISQUS 5 Comments

Please use the support forums for discussing the project, asking questions or posting bug-fixes!

Sort:
Comments 1 – 5 of 5:
  • First
  • ‹ Prev
  • Next ›
  • Last
  • reply
    Avatar
    kghastie
    said 4 years ago (1 Point)

    shouldn't

    Buyer.implement(Options);

    in the 1.11 code be:

    Buyer.implement(new Options);

    ?

  • reply
    Avatar
    said 4 years ago (1 Point)

    Very cool stuff! I will definitely follow your posts comparing 1.11 and 1.2. I've been a MooTools user for about a year now and am thrilled by all of the excellent improvements coming out in this latest release.

    I posted an article highlighting some of the new features and improvements to the codebase, as well as development methodologies (behavior driven development, specifically):

    http://www.thetruetribe.com/20...

  • reply
    Avatar
    Orix 0 Point
    said 4 years ago (1 Point)

    Great stuff, you always write quality code. I am new to mootools and learn by reading stuff you have written.

    Just a little comment from a newbe: It would have been interesting to have written the same class without your Bounds (adding the .bind(this) at the right places...).

  • reply
    Avatar
    sim 0 Point
    said 4 years ago (2 Points)

    I have already discovered that the old Ajax classes have been replaced with the new Request classes - I have already worked out the basics of rewriting my code that uses the 1.1 classes but I am sure that an article about the Request classes would help many people.

  • reply
    Avatar
    danielskinner 1 Point
    said 4 years ago (1 Point)

    Awesome. Bounds should be shipped with Class.Extras! I'm looking forward to the rest of the series as I haven't been keeping up to date with the recent changes since beta 2.

    I'd love to see a post about using the released version of Swiff effectively with AS3.

    Daniel

Comments 1 – 5 of 5:
  • First
  • ‹ Prev
  • Next ›
  • Last

Post your comment

Please use the support forums for discussing the project, asking questions or posting bug-fixes!


Internet Consultant & Contractor

I'm available to combine forces with you and your team to find the most simple, elegant and convenient web solutions . I await your call.

If you just like my work and want to say Thank You, donate via PayPal or Amazon Wish List.

Developer Resources & Tools