[Proto-Scripty] Re: Stoping observing

2009-01-15 Thread Alex Mcauley

Thanks TJ i didnt think about it like that, the second way would work ... 
its oky now as i just mke it fade away after 10 seonds or something

Thanks again

Alex
- Original Message - 
From: "T.J. Crowder" 
To: "Prototype & script.aculo.us" 
Sent: Thursday, January 15, 2009 4:26 PM
Subject: [Proto-Scripty] Re: Stoping observing



Hi Alex,

Apologies if I'm missing the point, but can't you use
document.stopObserving[1]?

[1] http://prototypejs.org/api/document/stopObserving

E.g.:
* * * *
function showErrorDiv(msg) {
var div = new Element('div', {
'id':'errDiv',
'class': 'error'
});
div.update(msg);
document.body.appendChild(div);
Element.observe.defer(document, 'click', removeErrDiv);
}

function removeErrDiv() {
var div = $('errDiv');
if (div) {
div.remove();
}
document.stopObserving('click', removeErrDiv);
}
* * * *

Or if you really need it to be a closure and not use a specific ID:
* * * *
function showErrorDiv(msg) {
var div = new Element('div', {
'class': 'error'
});
div.update(msg);
document.body.appendChild(div);
Element.observe.defer(document, 'click', function(){
try {
div.remove();
} catch (e) {}
document.stopObserving('click', arguments.callee);
});
}
* * * *

The defer is there in my case because I was using a click event to
trigger showErrorDiv, and of course because of bubbling that same
click eventually got given to the document and removed the div right
away -- deferring it lets me not worry about that.  (I could have just
stopped it in my click handler on my test button; either works.)

FWIW,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available

On Jan 15, 2:11 pm, Jeztah  wrote:
> Afternoon Guys / Gals ..
>
> I'm having a slight headache with stop observing 
>
> i'll try to explain my code ...
>
> upon click of an element (in this case a form submit) my javascript
> goes through some calculations and if needed calls a function to
> create an error message .. this function creates a fixed position div
> ontop of the page with the error message inside it.
>
> Now doing all this is fine but i need an
> Event.observe(document,'click', function() {
> removeTheElementIJustAdded();}
>
> on the end of it .
>
> this also works well and fine BUT !!  do i need to stop observing
> the document click else it will try to call that function every
> time? 
>
> P.S i cannot stop all click observers because i have more on the page
> that i need to listen to 
>
> All i want to do is simple .. create the div (done) and watch for a
> click event on the entire document that removes it
>
> but it seems to be a bit headachy for me for some reason.!!! - i think
> its due to me not wanting to have multiple Event.observe;s created!!
>
> Regards
> Alex



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Stoping observing

2009-01-15 Thread T.J. Crowder

Hi Alex,

Apologies if I'm missing the point, but can't you use
document.stopObserving[1]?

[1] http://prototypejs.org/api/document/stopObserving

E.g.:
* * * *
function showErrorDiv(msg) {
var div = new Element('div', {
'id':'errDiv',
'class': 'error'
});
div.update(msg);
document.body.appendChild(div);
Element.observe.defer(document, 'click', removeErrDiv);
}

function removeErrDiv() {
var div = $('errDiv');
if (div) {
div.remove();
}
document.stopObserving('click', removeErrDiv);
}
* * * *

Or if you really need it to be a closure and not use a specific ID:
* * * *
function showErrorDiv(msg) {
var div = new Element('div', {
'class': 'error'
});
div.update(msg);
document.body.appendChild(div);
Element.observe.defer(document, 'click', function(){
try {
div.remove();
} catch (e) {}
document.stopObserving('click', arguments.callee);
});
}
* * * *

The defer is there in my case because I was using a click event to
trigger showErrorDiv, and of course because of bubbling that same
click eventually got given to the document and removed the div right
away -- deferring it lets me not worry about that.  (I could have just
stopped it in my click handler on my test button; either works.)

FWIW,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available

On Jan 15, 2:11 pm, Jeztah  wrote:
> Afternoon Guys / Gals ..
>
> I'm having a slight headache with stop observing 
>
> i'll try to explain my code ...
>
> upon click of an element (in this case a form submit) my javascript
> goes through some calculations and if needed calls a function to
> create an error message .. this function creates a fixed position div
> ontop of the page with the error message inside it.
>
> Now doing all this is fine but i need an
> Event.observe(document,'click', function() {
>     removeTheElementIJustAdded();}
>
> on the end of it .
>
> this also works well and fine BUT !!  do i need to stop observing
> the document click else it will try to call that function every
> time? 
>
> P.S i cannot stop all click observers because i have more on the page
> that i need to listen to 
>
> All i want to do is simple .. create the div (done) and watch for a
> click event on the entire document that removes it
>
> but it seems to be a bit headachy for me for some reason.!!! - i think
> its due to me not wanting to have multiple Event.observe;s created!!
>
> Regards
> Alex
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Stoping observing

2009-01-15 Thread Alex Mcauley

I scrapped the idea and made it dissapear after a certain time .. was to 
much a headache !!
- Original Message - 
From: "bflanagan" 
To: "Prototype & script.aculo.us" 
Sent: Thursday, January 15, 2009 2:38 PM
Subject: [Proto-Scripty] Re: Stoping observing


>
> There's probably a correct event model way to do this, but, in the
> meantime, how about setting the 'removeErrorElement' equal to an empty
> function?
>
> >
> 


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Stoping observing

2009-01-15 Thread bflanagan

There's probably a correct event model way to do this, but, in the
meantime, how about setting the 'removeErrorElement' equal to an empty
function?

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Stoping observing

2009-01-15 Thread Alex Mcauley

The div isnt allways created by click .. its called by my method/function if 
and when i need it .. this is the trouble i am having ...
At the moment ive got the method to check for a click (in my tests) and it 
finds the div (as you have put below - this was my first approach at it 
yesterday !!)... but it gets made by the function making it then immediatly 
removed by the remove handler function

// by now the element is built so observe the document
Event.observe(document, 'click', removeErrorElement);

 },
 removeErrorElement : function() {
  if($('errortooltip')) {  // always locates because in my test i call it 
from a click
  $('errortooltip').remove(); // always removes because in my test i 
call it from a click
 //alert('Found Element');// always alerts because in my test i call it 
from a click
  Event.stopObserving(document, 'click', removeErrorElement); // what i 
want to do here is stop observing the document for a click for that specific 
function and listen for everything else .. if this makes sense !

}



Thanks
Alex

- Original Message - 
From: "bflanagan" 
To: "Prototype & script.aculo.us" 
Sent: Thursday, January 15, 2009 2:23 PM
Subject: [Proto-Scripty] Re: Stoping observing


>
> Since your function is creating the div, you can put a conditional in
> it to check for the existence of the div before attempting to add it.
>
> Event.observe(document,'click', function() {
>if($('divYouJustAdded'){
>   removeTheElementIJustAdded();
>} else {
>   addTheElement();
>}
> }
>
> >
> 


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Stoping observing

2009-01-15 Thread bflanagan

Since your function is creating the div, you can put a conditional in
it to check for the existence of the div before attempting to add it.

Event.observe(document,'click', function() {
if($('divYouJustAdded'){
   removeTheElementIJustAdded();
} else {
   addTheElement();
}
}

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---