On Mon, Feb 21, 2011 at 11:19 PM, Mark McDonnell <[email protected]> wrote:
> Hi there,
> I need help understanding the Adapter design pattern so I can help
> standardise the API usage on a library I'm working on.
> Let me clarify that I mention the Adapter design pattern but if there is
> another way to do what I want to do then please feel free to mention those
> alternatives!
> What I'm trying to achieve is if the browser uses the attachEvent method
> then to adapt the API so the user only has to write out a standard
> addEventListener method but really this passes onto the attachEvent
> function.
> Yes you could write an API like myapp.addEvent = function(){ /* code
> branching here */ } but I'm not looking for that. I'm looking for something
> that might not even be possible :)
> The problem I have found is that I have no way to completely integrate a
> missing method such as the addEventListener method. So for example if I do
> window.addEventListener = function(){ /* call attachEvent and pass through
> arguments */ } then that would only work for when the user writes
> window.addEventListener and not something
> like document.getElementById('test').addEventListener('click', function(e){
> console.log(e); }, false);
> So how would you try and accommodate every possible element.addEventListener
> variant?
> BTW I appreciate that this may not even be possible, I just wanted to mess
> around with it a bit and see what fun stuff I could play with.
> I was thinking I could try looping through the window object adding the
> method but that could potentially take 'ages'.
> I also appreciate that there is a right and wrong way to do things and that
> a lot of people will say 'thats wrong' but I'm more interested in whether
> it's possible to do rather than arguments about extending host objects is
> bad, etc etc which I'm aware of already.
> Hope this makes sense, and thanks for any help - if any there is to have
> from this :)
> Thanks.
> Mark
> --
>
> --
> To view archived discussions from the original JSMentors Mailman list:
> http://www.mail-archive.com/[email protected]/
>
> To search via a non-Google archive, visit here:
> http://www.mail-archive.com/[email protected]/
>
> To unsubscribe from this group, send email to
> [email protected]
>

Mark,
use event "delegation" and only setup one listener on the "document"
for the type of event you want notifications.

This will get you a notification for each click on each element:

    // for so called W3C browsers (those following the W3C specifications)
    document.addEventListener('click', function(event) {
console.log(event.target); }, false);

    // for IE 6 7 8 and browser using their engine
    document.attachEvent('onclick', function(event) {
console.log(event.target); });

    // best cross-browser solution and fall back for DOM 0 only
browsers (inexistent)
    document.onclick = function(event) { console.log((event ||
window.event).target); };

In the event handler check that the element correspond to one of those
you expect and process...

In this way you have the absolute fastest pattern and at the same time
you avoid extending objects.

The technique you are probably looking at is extending Window,
HTMLDocument, HTMLElement ... through their prototype but I suggest
you don't do this if you have alternatives and I assume you have a
very good one now ;-)

--
Diego

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to