Search code examples
inheritancejoomlamootools

Mootools 1.4.5 + Fail to execute the initialize method of a subclass


I have migrated a Joomla 1.5 website to Joomla 2.5.

The Joomla 1.5 website uses Mootools 1.11.
The Joomla 2.5 website uses Mootools 1.4.5.
The website contains a specific functionality named annuary.

The annuary requires some JavaScript files which are based on Mootools 1.11.
I need to adapt some instructions in these files to Mootools 1.4.5.
One of these JavaScript files is Autocompleter.js.
Here is an extract from Autocompleter.js :

var Autocompleter = {};

Autocompleter.Base = new Class({

    ...

    initialize: function(el, options) {

        this.setOptions(options);
        this.element = $(el);
        this.build();
        this.observer = new Observer(this.element, this.prefetch.bind(this), $merge({
        delay: 400
        }, this.options.observerOptions));
        this.value = this.observer.value;
        this.queryValue = null;
    },

    ...

});

Autocompleter.Base.implement(new Events);

Autocompleter.Base.implement(new Options);

Autocompleter.Local = Autocompleter.Base.extend({

    ...

    initialize: function(el, tokens, options) {

        this.parent(el, options);
        this.tokens = tokens;
        if (this.options.filterTokens) this.filterTokens = this.options.filterTokens.bind(this);
    },

    ...

});

I have noticed that the following JavaScript instruction relative to annuary does not work as expected anymore :

new Autocompleter.Local(idChamp, tabValues, {

    'delay': 100,
    'injectChoice': function(choice) {
        var el = new Element('li')
        .setHTML(this.markQueryValue(choice[0]))
        .adopt(new Element('span', {'class': 'example-info'}).setHTML(this.markQueryValue(choice[1])));
        el.inputValue = choice[0];
        this.addChoiceEvents(el).injectInside(this.choices);
    }
});

The initialize function of Autocompleter.Base is executed.
But the initialize function of Autocompleter.Local is not.
Could someone explain me why ?
I am convinced the problem is caused by the use of Mootools 1.4.5.


Solution

  • This takes me back...

    In 1.2.5 Class got quite a revamp. Instead of doing the prototype and then calling implement / extend, you now do these as mutators

    Autocompleter.Base = new Class({
    
        Implements: [Options, Events],
    
        initialize: function(el, options) {
    
            this.setOptions(options);
            this.element = $(el);
            this.build();
            this.observer = new Observer(this.element, this.prefetch.bind(this), $merge({
            delay: 400
            }, this.options.observerOptions));
            this.value = this.observer.value;
            this.queryValue = null;
        },
    
        ...
    
    });
    
    Autocompleter.Local = new Class({
        Extends: Autocompleter.Base
    });
    

    You should read some of the guides/tutorials on moving from 1.11 to 1.2.5. and 1.2.5 is actually not a good release to be on right now, its 2.5 yrs old and is currently broken in FireFox 18 due to https://bugzilla.mozilla.org/show_bug.cgi?id=789036

    see some tutorial on writing classes now like http://fragged.org/tutorial-write-a-small-content-slider-class-in-mootools-and-extend-it_1321.html etc