Newbie alert! So let''s I have this in my javascript Event.observe(''myLinkId'', ''onclick'', myFunctionName); or even $(''myDivID'').hide what if they don''t exist yet? For example i am pulling in content via ajax.updater so some of my elements do not exist when the javascript is loaded. So all my javascript fails and the page does not function because it looks for those elements and does not find them. So do i handle this? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
ESPNDev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Feb-10 03:42 UTC
Re: What if my element doesn''t exist yet
I''m not really sure how you''re code is set up, but the most generic way would be use setInterval and clearInterval when the condition is met IE function setObservers(){ if( $(''myLinkId'') ){ Event.observe(''myLinkId'', ''onclick'', myFunctionName); clearInterval(timer); } } var timer = setInterval(''setObservers'',100); It''s not the most elegant solution, but w/o knowing exactly what you''re trying to do, it''s the best i can offer. On Feb 9, 7:22 pm, anathema <spamfreeunive...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Newbie alert! > > So let''s I have this in my javascript > > Event.observe(''myLinkId'', ''onclick'', myFunctionName); > > or even > > $(''myDivID'').hide > > what if they don''t exist yet? For example i am pulling in content via > ajax.updater so some of my elements do not exist when the javascript > is loaded. So all my javascript fails and the page does not function > because it looks for those elements and does not find them. > > So do i handle this?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Well let me give you a little better explanation. initially a page is loaded... let''s say the home page. from there people use the main navigation. when a link is clicked i get the page through ajax.update with fancy fades and stuff. all is good because that nav exists on every page so the elements are always present. however, on one of the ''pages'' that i retrieve via ajax there is a sub nav. so now i have all these new elements in which i want to do the same thing as i did the main nav. click on link and content is replaced. did that make sense? On Feb 9, 7:42 pm, "ESPN...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <ESPN3.DL- Community...-xX8xgfAcNKEAvxtiuMwx3w@public.gmane.org> wrote:> I''m not really sure how you''re code is set up, but the most generic > way would be use setInterval and clearInterval when the condition is > met > > IE > > function setObservers(){ > if( $(''myLinkId'') ){ > Event.observe(''myLinkId'', ''onclick'', myFunctionName); > clearInterval(timer); > } > > } > > var timer = setInterval(''setObservers'',100); > > It''s not the most elegant solution, but w/o knowing exactly what > you''re trying to do, it''s the best i can offer. > > On Feb 9, 7:22 pm, anathema <spamfreeunive...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Newbie alert! > > > So let''s I have this in my javascript > > > Event.observe(''myLinkId'', ''onclick'', myFunctionName); > > > or even > > > $(''myDivID'').hide > > > what if they don''t exist yet? For example i am pulling in content via > > ajax.updater so some of my elements do not exist when the javascript > > is loaded. So all my javascript fails and the page does not function > > because it looks for those elements and does not find them. > > > So do i handle this?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 9, 2008 10:22 PM, anathema <spamfreeuniverse-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Newbie alert! > > So let''s I have this in my javascript > > Event.observe(''myLinkId'', ''onclick'', myFunctionName); > > or even > > $(''myDivID'').hide > > what if they don''t exist yet? For example i am pulling in content via > ajax.updater so some of my elements do not exist when the javascript > is loaded. So all my javascript fails and the page does not function > because it looks for those elements and does not find them. > > So do i handle this?first off you should be using dom level 2 events; so onclick should be click. second, if the element w/ id, myLinkId, doesnt exist until its placed in the dom by Ajax.Updater, then simply include it in a script tag in the response from the server, <script> Event.observe(''myLinkId'', ''click'', myFunctionName); </script> then set evalScripts:true in the Ajax.Options when you instantiate Ajax.Updater and you should be good to go ;) -nathan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
ESPNDev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2008-Feb-10 05:55 UTC
Re: What if my element doesn''t exist yet
Yup. That makes sense. So what you want to do is not call Event.observe on page load but, after the Ajax call has been completed and the html inserted. You may not be able to use the updater method, since you need to coordinate the call. Instead, use Ajax.Request and manually update the subnav with the $ (''container'').update(html) method. After that, THEN call Event.Observe. On Feb 9, 8:38 pm, anathema <spamfreeunive...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Well let me give you a little better explanation. > > initially a page is loaded... let''s say the home page. from there > people use the main navigation. when a link is clicked i get the page > through ajax.update with fancy fades and stuff. > > all is good because that nav exists on every page so the elements are > always present. however, on one of the ''pages'' that i retrieve via > ajax there is a sub nav. so now i have all these new elements in which > i want to do the same thing as i did the main nav. click on link and > content is replaced. > > did that make sense? > > On Feb 9, 7:42 pm, "ESPN...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org" <ESPN3.DL-Community...-xX8xgfAcNKEAvxtiuMwx3w@public.gmane.org> wrote: > > I''m not really sure how you''re code is set up, but the most generic > > way would be use setInterval and clearInterval when the condition is > > met > > > IE > > > function setObservers(){ > > if( $(''myLinkId'') ){ > > Event.observe(''myLinkId'', ''onclick'', myFunctionName); > > clearInterval(timer); > > } > > > } > > > var timer = setInterval(''setObservers'',100); > > > It''s not the most elegant solution, but w/o knowing exactly what > > you''re trying to do, it''s the best i can offer. > > > On Feb 9, 7:22 pm, anathema <spamfreeunive...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Newbie alert! > > > > So let''s I have this in my javascript > > > > Event.observe(''myLinkId'', ''onclick'', myFunctionName); > > > > or even > > > > $(''myDivID'').hide > > > > what if they don''t exist yet? For example i am pulling in content via > > > ajax.updater so some of my elements do not exist when the javascript > > > is loaded. So all my javascript fails and the page does not function > > > because it looks for those elements and does not find them. > > > > So do i handle this?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 10, 2008 12:55 AM, ESPNDev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org <ESPN3.DL-CommunityDev-xX8xgfAcNKEAvxtiuMwx3w@public.gmane.org> wrote:> > Yup. That makes sense. So what you want to do is not call > Event.observe on page load but, after the Ajax call has been completed > and the html inserted. > > You may not be able to use the updater method, since you need to > coordinate the call. > > Instead, use Ajax.Request and manually update the subnav with the $ > (''container'').update(html) method. After that, THEN call > Event.Observe.using Ajax.Updater together w/ the evalScripts option will do just that, and it will be simpler to implement. -nathan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Or, you could add to the onComplete handler on the Ajax.Updater new Ajax.Updater(container, url, { onComplete: function(t) { $("link").observe("click", handler); } }); This will keep the javascript in one place, and will be called only after the content has been pulled into the page. Get to know all the callbacks and options you can pass to the Ajax.Request and Ajax.Updater objects: http://prototypejs.org/api/ajax/options Best, -Nicolas On Feb 10, 2008 4:12 AM, Nathan Nobbe <quickshiftin-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Feb 10, 2008 12:55 AM, ESPNDev-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org > <ESPN3.DL-CommunityDev-xX8xgfAcNKEAvxtiuMwx3w@public.gmane.org> wrote: > > > > > Yup. That makes sense. So what you want to do is not call > > Event.observe on page load but, after the Ajax call has been completed > > and the html inserted. > > > > You may not be able to use the updater method, since you need to > > coordinate the call. > > > > Instead, use Ajax.Request and manually update the subnav with the $ > > (''container'').update(html) method. After that, THEN call > > Event.Observe. > > using Ajax.Updater together w/ the evalScripts option will do just that, > and it will be simpler to implement. > > -nathan > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Or you could use event delegation which doesn''t really care if element exists. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 10, 2008 9:30 AM, kangax <kangax-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Or you could use event delegation which doesn''t really care if element > exists.are you suggesting to use the custom events in 1.6.0 whereby the original page load would observe an (custom) event that would then be fired by the Ajax.Updater? or have i completely missed the boat :)? -nathan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks for all the replies, although I don''t know how to go about implementing some of them. What is event delegation? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Event delegation is a technique of observing events on an ancestor element (positioned "higher" in DOM) and exploiting events'' bubbling nature (ability of ancestor to catch event originated on its descendant). It all just sounds complicated but essentially means this: document.observe(''click'', function(e) { if (e.findElement(''#myLinkId'')) myFunction(e); }) Now myFunction will be called only if user clicks on an element with specified id (and simply ignoring everything else) Hope this helps. Best, kangax --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
this is probably going to sound stupid, but what the ''e'' represent? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
On Feb 10, 2008 6:44 PM, anathema <spamfreeuniverse-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > this is probably going to sound stupid, but what the ''e'' represent?if you are talking about the code kangax posted, document.observe(''click'', function(e) { if (e.findElement(''#myLinkId'')) myFunction(e); }) the function in the second argument to document.observe() is anonymous. its designed to be called at a later point in time. when it does get called, it will get a reference passed to it, an event, thats what e is in this case. -nathan --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
Sorry, I don''t get it. Does it have to be an ''e'', or did he just choose that letter randomly? Maybe a complete example would help. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---
the e is the argument name on the function and what the event object becomes known as within the scope of the function. it can be anything, but most of us use ''e'' as it''s short and then you can copy/paste code easily. if you prefer you can call it evt or something.. prototype passes the event object as that argument to the function, like this: definition: function dosomething(somearg) { alert(somearg) } call: dosomething(''hi''); function arguments in scope: somearg => "hi" output: alert box of "hi" if you take this example and treat somearg as e and "hi" as an event object.. the same applies On Feb 11, 2008 1:52 PM, anathema <spamfreeuniverse-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Sorry, I don''t get it. Does it have to be an ''e'', or did he just > choose that letter randomly? Maybe a complete example would help. > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Spinoffs" group. To post to this group, send email to rubyonrails-spinoffs-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-spinoffs-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---