Implementing jQuery’s “One” Functionality in MooTools

By  on  

I like the idea behind jQuery's "one" event method. You allow an event function to be triggered once per Element; after its initial run, the function is removed, never to be heard from again (a.k.a until the next page load). I think there are a lot of use cases for "one" so I've ported it to MooTools.

The MooTools JavaScript

/* one-time event occurrence */
Element.implement({
	one: function(ev,fn) {
		if(!this.oneEvents) { this.oneEvents = []; }
		var oneFunc = function(e) {
			if(!this.oneEvents.contains(fn)) {
				this.oneEvents.push(fn);
				fn.run(e);
			}
			return this;
		};
		this.addEvent(ev,oneFunc);
	}
});

The way I complete this is by storing all of the one events in the element's oneEvent property. I wrap the given function in another function that checks to see if the given function has been used yet. If the function hasn't been run, the event triggers it. If the function has been run, the function directs itself to ignore the occurrence.

The Usage

/* usage */
window.addEvent('domready',function() {
	var myFunc = function(e) {
		e.stop();
		alert('triggered!');
	};
	$$('a').each(function(link) {
		link.one('click',myFunc);
	});
});

I chose this example to show that the event will fire only once per element BUT will fire for each element if the same function is passed to multiple event.

Would you use this Element / Event method?

Recent Features

  • By
    9 Mind-Blowing WebGL Demos

    As much as developers now loathe Flash, we're still playing a bit of catch up to natively duplicate the animation capabilities that Adobe's old technology provided us.  Of course we have canvas, an awesome technology, one which I highlighted 9 mind-blowing demos.  Another technology available...

  • By
    Responsive and Infinitely Scalable JS Animations

    Back in late 2012 it was not easy to find open source projects using requestAnimationFrame() - this is the hook that allows Javascript code to synchronize with a web browser's native paint loop. Animations using this method can run at 60 fps and deliver fantastic...

Incredible Demos

  • By
    Fading Links Using jQuery:  dwFadingLinks

    UPDATE: The jQuery website was down today which caused some issues with my example. I've made everything local and now the example works. Earlier this week, I posted a MooTools script that faded links to and from a color during the mouseover and mouseout events.

  • By
    Build a Calendar Using PHP, XHTML, and CSS

    One of the website features my customers love to provider their web users is an online dynamic calendar. An online calendar can be used for events, upcoming product specials, memos, and anything else you can think of. I've taken some time to completely...

Discussion

  1. Really cool idea, and nicely implemented. I’m not sure that I’d use that method name though – ‘one’ could mean anything. Maybe addEventOnce..?

  2. Hey David, I have a different implementation, which I called oneEvent(), that I’d like you to see. kthxbai

  3. You can do with removeEvent(). Simpler, faster & less code.

  4. Do you have the code somewhere Oskar?

  5. something like this?

    Element.implement({
        one:function(ev,fn){
            var oneFunc = function(e){
                fv.run(e,this);
                this.removeEvent(ev,oneFunc);
            }.bind(this);
            this.addEvent(ev,oneFunc);
            return this;
        }
    });
    
  6. should be fn not fv on the fourth line

  7. should also get the .live functionality added to mootools also, I saw it here: http://dev.k1der.net/dev/live-events-pour-moootools/

  8. The implementation Scott shared is exactly what I would expect to do. Simply wrap the event listener to remove the event when it’s fired.

  9. UPDATE: Aaron Newton mentioned that my above implementation would create a memory leak and give problems with “removeEvents”, so I may update my code above. Scott’s solution above is likely the best solution.

  10. Jesus DeLaTorre

    Would you mind sharing with us how this can create a memory leak?

  11. danik

    Thanks. I use both jQuery and Mootools, and sometimes need to “port” some code from one to other. Now it became simpler.

  12. Updating an old post…
    MooTools doesn’t use Native anymore, I had to change Scott’s solution first line to make it work (if you are not using compatibility mode in MooTools).

    [Element, Window, Document].invoke('implement', {
    

    Instead of

    Native.implement([Element, Window, Document, Events], {
    

    Also, Google’s hosted library (1.4.5) seems to be using compatibility mode.

    Feel free to correct me if I’m doing something wrong here.
    Final code :

    [Element, Window, Document, Events].invoke('implement', {
      one : function(type, fn) {
    	return this.addEvent(type, function() {
    	  this.removeEvent(type, arguments.callee);
    	  return fn.apply(this, arguments);
    	});
      }
    });
    

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