OK on using addMethods. Makes sense, and it's working for me. Very nice. Thanks.
About not assigning functions after creation without a good factory method, I'm confused. Isn't that what the example is doing on the Prototype example at: http://prototypejs.org/learn/class-inheritance ? // define a module var Vulnerable = { wound: function(hp) { this.health -= hp; if (this.health < 0) this.kill(); }, kill: function() { this.dead = true; } }; // the first argument isn't a class object, so there is no inheritance ... // simply mix in all the arguments as methods: var Person = Class.create(Vulnerable, { initialize: function() { this.health = 100; this.dead = false; } }); var bruce = new Person; bruce.wound(55); bruce.health; //-> 45 On Jul 13, 4:22 pm, Matt Foster <[email protected]> wrote: > The second argument is accepting attributes of the HTML element. It > isn't just assigning values to the object like Object.extend style. > > I'd recommend using Element.addMethods, and scope it to only canvas > elements, that way each time you create a canvas you'll have this > method available, but it won't pollute other elements with this > special functionality. > Element.addMethods('canvas', ...); > > http://prototypejs.org/api/element/addMethods > > You could also assign the function value after creation, but unless > you're using a good factory pattern, you could easily get instances of > canvas without your specialized method. You can't send methods to the > constructor like that, just won't work. The declaration for Element > is a bit bizarre so it can be hard to find, so here it is... > > (function() { > var element = this.Element; > this.Element = function(tagName, attributes) { > attributes = attributes || { }; > tagName = tagName.toLowerCase(); > var cache = Element.cache; > if (Prototype.Browser.IE && attributes.name) { > tagName = '<' + tagName + ' name="' + attributes.name + '">'; > delete attributes.name; > return Element.writeAttribute(document.createElement(tagName), > attributes); > } > if (!cache[tagName]) cache[tagName] = Element.extend > (document.createElement(tagName)); > return Element.writeAttribute(cache[tagName].cloneNode(false), > attributes); > }; > Object.extend(this.Element, element || { }); > if (element) this.Element.prototype = element.prototype; > > }).call(window); > > -- > > http://positionabsolute.net > > On Jul 13, 2:40 pm, ronman <[email protected]> wrote: > > > Why doesn't 'getCoordinates' show up in the following code? > > canvas.getCoordinates() throws an error of "getCoordinates is not a > > function" > > > I'm making a newbie error somewhere. > > > var canvas = new Element('canvas', { > > width: this.xform(data.size[0]) + 16 + 'px', > > height: this.xform(data.size[1]) + 16 + 'px', > > > // mixin to Element for canvases > > getCoordinates: function(element){ > > var position = this.getPosition(element), size = this.getSize > > (); > > var obj = {left: position.x, top: position.y, width: size.x, > > height: size.y}; > > obj.right = obj.left + obj.width; > > obj.bottom = obj.top + obj.height; > > return obj; > > }, > > } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Prototype & script.aculo.us" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/prototype-scriptaculous?hl=en -~----------~----~----~----~------~----~------~--~---
