Matthew C wrote: > I have some code that creates several observers for elements on a > page. Due to my application logic, those elements may or may not be > there. So I was wrapping them in conditional logic. To make that logic > easier I implemented the following: > > Object.extend(Event, { > observe_if_present: function(element,eventName,handler) { > Object.isElement(element) ? > Event.observe(element,eventName,handler) : false > } > }) > > First of all, is this a good idea? Is there a better way to initialize > observers for elements that may or may not exist? > > Thanks, > > Matt Consider using $$() and invoke() or event delegation (http://scripteka.com/script/element-delegate). See example below.
- Ken Snyder // assign the class "modalPopup" to all elements that should popup a modal window $$('.modalPopup').invoke('observe', 'click', showModal); // if no elements with that class exists, no observers will be attached // similar example with event delegation so that there is only one observer $('myPage').delegate('click', '.modalPopup', showModal); --~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/rubyonrails-spinoffs?hl=en -~----------~----~----~----~------~----~------~--~---