Just a simple class i did at work, i do …


Just a simple class i did at work, i do mostly flash so when i code in javascript sometimes i need to add events and code more like i code in as3.

In case someone find this usefull, just a simple class, needs JQUERY to work.

// Signal.js
function Signal( str )
{
    var listeners = [];

    this.dispatch = function()
    {
        for( var i = 0; i<listeners.length; i++ )
        {
            listeners[i].apply( null, arguments );
        }
    }

    this.listen = function( callback )
    {
        if( $.inArray( callback, listeners )==-1 )
        {
            listeners.push( callback );
        }
    }

    this.remove = function( callback )
    {
        listeners = $.grep( listeners, function(v)
                    {
                        return v != callback;
                    });
    }

    this.removeAll = function()
    {
        listeners.length = 0;
    }

}

 

Advertisements