YUILibrary - Open source JavaScript and CSS for building richly interactive software.
Fork YUI on GitHub
  [ 4 posts ]

Daniel Cook

  • Offline
  • Profile

Events: unsubscribe after one firing?

Post Posted: Tue Feb 09, 2010 2:11 pm
+0-
I am using the IO module as suggested in the documentation and I am passing through a few arguments. The problem lies in the fact that if I rerun this code a few times, I am subscribing multiple times to the 'io:complete' event with different arguments. The solution I came up with was to store the subscription in a variable and then, later, detach but that is rather cumbersome. Any ideas?
Code:
var eventSubscription;
function sendData(callback){
       eventSubscription = Y.on('io:complete', handleResponse , this, {callback: callback} );
   var request = Y.io('somesite.com');   
}

function handleResponse(trans, response, args){
    eventSubscription.detach();
    args.callback();
}

sendData(function(){
alert('first');
});

sendData(function(){
alert('second');
});
-dcunited

Dav Glass

YUI Developer

  • Username: davglass
  • Joined: Thu Aug 28, 2008 9:28 am
  • Posts: 1156
  • Location: Cupertino, CA
  • Twitter: davglass
  • GitHub: davglass
  • Gists: davglass
  • IRC: davglass
  • YUI Developer
  • Offline
  • Profile

Re: Events: unsubscribe after one firing?

Post Posted: Tue Feb 09, 2010 2:14 pm
+0-
You don't have to listen globally, the examples on the IO landing page just don't show the transactional event listeners.

This example shows both:
http://developer.yahoo.com/yui/3/exampl ... o-get.html

In your case you want to listen on each transaction and not globally.

Daniel Cook

  • Offline
  • Profile

Re: Events: unsubscribe after one firing?

Post Posted: Wed Feb 10, 2010 7:29 am
+0-
Thanks Dav.

Adrian Ziemkowski

  • Username: ziemkowski
  • Joined: Thu Sep 17, 2009 11:10 am
  • Posts: 5
  • Twitter: ziemkowski
  • Offline
  • Profile

Re: Events: unsubscribe after one firing?

Post Posted: Wed Feb 10, 2010 12:56 pm
+3-
davglass wrote:
In your case you want to listen on each transaction and not globally.


The idea is still nice though. I was just checking the API for something like a Y.once() a few days ago while refactoring some jQuery code that was using their one() method to execute some code just once after a particular ajax call had run (sort of like an onready for an iframe, but for a div with dynamic content).

Wasn't much to work around it of course, but it would be a nice shortcut for sure, especially with quick anonymous functions.

Didn't find a ticket for something like this, so I created ticket 2528618

Update: This is coming in YUI 3.1.0 (see ticket for details)
  [ 4 posts ]
Display posts from previous:  Sort by  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum