Destroying Observers

15 views
Skip to first unread message

atlguy

unread,
Sep 23, 2011, 10:14:01 AM9/23/11
to Blast Mojo
Is there a way to destroy an observer outside of the Mojo framework? I
have an inline JS function that checks for a specific condition (needs
to be inline) and if that condition is met, I need to destroy an
observer.

Thanks!

Jaime Bueza

unread,
Sep 23, 2011, 12:48:29 PM9/23/11
to blast...@googlegroups.com
You sure can, if you can get a reference to the Controller object on a context element, you can call removeObservers() on it:


Although, I just noticed, it removes all observers from a particular Controller instead of a specific one. 

Hope that helps!

Cheers,
Jaime

Jaime Bueza Software Engineer
LinkedIn | Twitter | Google Plus | Blog | Github

Loyal Chow

unread,
Sep 23, 2011, 2:40:02 PM9/23/11
to blast...@googlegroups.com
a clean way to do it completely outside of mojo may be to take advantage of its event delegation

if you observe using just a string selector, mojo will automatically use event delegation on events that bubble (click, mouseover, etc.). You can use a particular class in your mojo observer, and then just remove that class in your custom JS function to no longer observe it.

Example:
<a href="#" class="someClass">test</a>

// using event delegation
this.addObserver(".someClass", "onclick", "DoSomething");

Remove the "someClass" class from the anchor tag, and the observer will no longer fire.
Note: this only works on events that bubble

Cheers,

Loyal



On Fri, Sep 23, 2011 at 7:14 AM, atlguy <rob...@sekman.com> wrote:

Jaime Bueza

unread,
Sep 27, 2011, 10:06:42 PM9/27/11
to blast...@googlegroups.com
Hey Robert,

Just following up, did you end up with a solution were you satisfied with? I'd personally go with Loyal's solution as it does keep things simple and you can easily add comments in regards to the case you're trying to write code against.

Cheers,
Jaime

atlguy

unread,
Sep 28, 2011, 8:56:42 AM9/28/11
to Blast Mojo
Not quite yet. My event doesn't "bubble" so I could not use that.
Removing the class did not remove the observer. I haven't quite
figured out how to make removeObservers() work on it.

Loyal Chow

unread,
Sep 28, 2011, 1:42:41 PM9/28/11
to blast...@googlegroups.com
Hey Robert,

to use "removeObservers", you need access to the controller object, which you can get from its context DOM element

here's an example:
var elm = mojo.queryFirst("#your-contextID");
elm.mojoControllers['some.controller.YourController'].removeObservers()

Cheers,

Loyal

atlguy

unread,
Sep 29, 2011, 1:25:32 PM9/29/11
to Blast Mojo
That did, although too well. Thank you! I really don't want to remove
all observers for the controller but I can find a way around that.
Reply all
Reply to author
Forward
0 new messages