event listener context

10 views
Skip to first unread message

Maciej Dziardziel

unread,
Nov 9, 2010, 5:09:20 PM11/9/10
to Socket.IO
Is there a way to pass context to event listener?

Something like:

socket.on("message", function(){}, {context: some_object } );

I am trying to pass class method (using JS.Class library) as a
listener,
but without context its a bit tricky.

--
Maciej Dziardziel

Tad Glines

unread,
Nov 9, 2010, 5:42:14 PM11/9/10
to sock...@googlegroups.com
Perhaps this is a naive solution but what about doing:

function bind(context, fn) () {
    return function() { fn.apply(context, arguments); };
}

socket.on("message", bind(object, function(message) { // do stuff });

I think dojo provides a function (bind?) to do this that is much more intelligent.

-Tad

Arnout Kazemier

unread,
Nov 9, 2010, 5:43:31 PM11/9/10
to sock...@googlegroups.com
I was about to suggest the same, nearly all decent js libraries support something like that. 

Maciej Dziardziel

unread,
Nov 9, 2010, 6:43:03 PM11/9/10
to Socket.IO
Thanks.

On Nov 9, 11:42 pm, Tad Glines <tad.gli...@gmail.com> wrote:
> socket.on("message", bind(object, function(message) { // do stuff });

-
Maciej Dziardziel
fie...@gmail.com

Julien Genestoux

unread,
Nov 9, 2010, 5:53:52 PM11/9/10
to sock...@googlegroups.com
UNSUBSCRIBE

James Pike

unread,
Nov 9, 2010, 5:36:40 PM11/9/10
to sock...@googlegroups.com
> Is there a way to pass context to event listener?
>
> Something like:
>
> socket.on("message", function(){}, {context: some_object } );

Use a closure

var context = {context: some_object}
socket.on("message", function(){
// function can access context
})

--
http://blaggart.com:8008

Reply all
Reply to author
Forward
0 new messages