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
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
In that situation I prefer to component-ize my page initializers. So I''ll have a global initializer that attaches observers to elements that are known to *always* exist, then other initializers that are invoked based on page content. Your solution works also, it''s a personal preference. -justin --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
It''s definitely better than writing "if .. observe" every time. I usually use: Event.observe(elementInQuestion, eventName, handler); which silently fails when element doesn''t exist. Otherwise, a slightly shorter: element.observe(eventName, handler); - kangax On Apr 28, 1:29 pm, Matthew C <desig...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
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, > > MattConsider 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-/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 -~----------~----~----~----~------~----~------~--~---