0

How to unregister a messagecenterlistener

I have a timer which is defined and kicked off in the init.js file , the timer basically makes a async request to an external website and gets a list of values which are then stored in message center.

In the updateview method of one of the views in the sidebar (it opens up when a button is clicked on the sidebar) I add the messagelistenercallback for that view (I believe it needs to be in the updateview function because a value is passed dynamically which is used in the call back function to check if the event is for this particular instance of the view).

Every thing works fine when I click on the button, the new view gets the events it discards unwanted events and extracts data from the event it is interested in.

Now when I press the back button, I want to unregister this call back (otherwise I see in the log file that the callback keep getting called even when it is not the current view on the side bar, which probably makes sense technically). I couldn't find the method to unregister the messagecenterlistener. Following are my questions

1. Is it a good practice to unregister the listener when the back button is pressed using the below code or should it be somewhere else

CODEBOX
createView: function() {
this.controls.backbutton = new KONtx.control.BackButton({
label: "My sub view",
events: {
onSelect: function(event) {
print("back button pressed!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
//unregister listenercallback here }
},

}).appendTo(this);


2. What is the method to unregister a messagecenterlistener

by
5 Replies
  • QUOTE (SajuP @ Feb 14 2011, 07:39 PM) <{POST_SNAPBACK}>
    1. Is it a good practice to unregister the listener when the back button is pressed using the below code or should it be somewhere else

    Unregistering any events that were registered in updateView can be done in hideView.

    QUOTE (SajuP @ Feb 14 2011, 07:39 PM) <{POST_SNAPBACK}>
    2. What is the method to unregister a messagecenterlistener

    In the case of registering events on a view, they are unregistered for you when the view is unloaded. Check out http://developer.yahoo.com/connectedtv/kon...le:core__system

    Out of curiosity, why are you using a timer? Are you polling?
    0
  • QUOTE (Benjamin Toll @ Feb 14 2011, 08:18 PM) <{POST_SNAPBACK}>
    Unregistering any events that were registered in updateView can be done in hideView.


    Thanks, this helps.

    QUOTE (Benjamin Toll @ Feb 14 2011, 08:18 PM) <{POST_SNAPBACK}>
    In the case of registering events on a view, they are unregistered for you when the view is unloaded. Check out http://developer.yahoo.com/connectedtv/kon...le:core__system


    Is there a KONTX API function which I can call to unregister it myself or I need to make use of the unregister which is called on the unload of the view.

    QUOTE (Benjamin Toll @ Feb 14 2011, 08:18 PM) <{POST_SNAPBACK}>
    Out of curiosity, why are you using a timer? Are you polling?


    Yes, I'm polling for new data updates from a website and the same is updated in the message center every time the timer triggers. Is this the right approach or is there a better way to do this?
    0
  • QUOTE (SajuP @ Feb 19 2011, 10:05 AM) <{POST_SNAPBACK}>
    Is there a KONTX API function which I can call to unregister it myself or I need to make use of the unregister which is called on the unload of the view.

    The registerMessageCenterListenerCallback method was added as a convenience so the views could listen to the message center and the developer wouldn't need to worry about cleaning up, as the framework takes care of this. Unfortunately, it doesn't seem like your use case fits well into this model. Perhaps you should stop the timer by setting its ticking property to false and then restart it elsewhere in your app. You could stop the timer as soon as the user leaves the view. This would work much better in your scenario, as you wouldn't be dependent upon an event to unregister your listener. In fact, you wouldn't need to register the callback in the first place, just set ticking to true/false as you need as the user navigates your app.

    QUOTE (SajuP @ Feb 19 2011, 10:05 AM) <{POST_SNAPBACK}>
    Yes, I'm polling for new data updates from a website and the same is updated in the message center every time the timer triggers. Is this the right approach or is there a better way to do this?

    I think polling is fine if your source data is dynamic. An alternative to polling is Comet. I've used it before in a test app, and it suited my needs.
    0
  • QUOTE (Benjamin Toll @ Feb 22 2011, 09:43 AM) <{POST_SNAPBACK}>
    Perhaps you should stop the timer by setting its ticking property to false and then restart it elsewhere in your app. You could stop the timer as soon as the user leaves the view. This would work much better in your scenario, as you wouldn't be dependent upon an event to unregister your listener.


    Thanks a lot, I think this might be a feasible workaround for me, I just need to think through and make sure it won't break anything else.

    QUOTE (Benjamin Toll @ Feb 22 2011, 09:43 AM) <{POST_SNAPBACK}>
    In fact, you wouldn't need to register the callback in the first place, just set ticking to true/false as you need as the user navigates your app.


    I might not be able to do this because the timer fetches data for multiple views which translates into different events for the dynamic buttons displayed in the first side bar view (the view which displays the data is opened up when user clicks on one of these buttons as explained in the original post).

    QUOTE (Benjamin Toll @ Feb 22 2011, 09:43 AM) <{POST_SNAPBACK}>
    I think polling is fine if your source data is dynamic. An alternative to polling is Comet. I've used it before in a test app, and it suited my needs.


    Thanks, will take a look at the Comet stuff, it seems interesting, will need to analyse to see if it is the best fit in terms of performance etc,.


    On a side note, is it possible to add a public unregistereventlistener method to the frame work in a future release?

    Ben, I really appreciate the prompt and to the point responses which I keep receiving in this forum, it makes things a loooot easier!!

    -Sajith
    0
  • QUOTE (SajuP @ Feb 23 2011, 07:46 PM) <{POST_SNAPBACK}>
    On a side note, is it possible to add a public unregistereventlistener method to the frame work in a future release?

    Duly noted.

    QUOTE (SajuP @ Feb 23 2011, 07:46 PM) <{POST_SNAPBACK}>
    Ben, I really appreciate the prompt and to the point responses which I keep receiving in this forum, it makes things a loooot easier!!

    -Sajith

    Thanks for the feedback!
    0

Recent Posts

in Getting Started / Beginners - Yahoo! TV Widgets