On Feb 22, 8:19 am, Mark McDonnell <storm.m...@gmail.com> 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 :)
Aren't "addEvent" functions an implementation of the adapter design pattern? Perhaps a "forking adapter pattern"? > 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? addEventListener is part of interface EventTarget[1] and is implemented by all Nodes (in W3C DOM compliant UAs). If all your target host environments implement prototype inheritance and make a suitable prototype publicly available, you could add a method to that prototype. In Firefox, there is Element.prototype, so: if (Element && Element.prototype) { if (typeof Element.prototype.addEventListener != 'function') { ... } } But that is a bad strategy for a number of reasons, such as: 1. Attempting to access any non-function property of Element.prototype in Firefox throws an illegal operation error. 2. IE doesn't expose any kind of prototype inheritance that can be used this way > > 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. Have a look at Prototype.js, it messes with host objects and their prototypes (where it can). That is a major reason why few who know scripting well recommended its use. I believe the maintainers are changing it for version 2.0 so that it no longer augments host objects. It took them a long time to accept that modifying Object.prototype was a bad idea, Array.prototype is still augmented. > I was thinking I could try looping through the window object adding the > method but that could potentially take 'ages'. You mean loop over every element in the DOM. Yes, it would take a while and besides, you only want the method on the elements that you wish to call it from. > > 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. See above. In Firefox and similar browsers you can do: Element.prototype.foo = function() { alert('foo!!'); } window.onload = function() { document.getElementById('someId').foo(); } 1. <URL: http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-EventTarget > -- Rob -- To view archived discussions from the original JSMentors Mailman list: http://www.mail-archive.com/jsmentors@jsmentors.com/ To search via a non-Google archive, visit here: http://www.mail-archive.com/jsmentors@googlegroups.com/ To unsubscribe from this group, send email to jsmentors+unsubscr...@googlegroups.com