Thanks for the brief explanation.. I can see how that could add some overhead.
As for what I'm trying... I guess the decorator pattern would be a good description. The initialization is a little more complicated than that however... What I'm looking for is a way to ensure that this one piece of code gets added at the end of the initialize method. The base class initialize has different code (for initializing the base class) which could get called at anytime in the subclass' initialize. What I need is to ensure that this set of code always gets called last. I'm thinking that the best way to do this is going to be to use your suggestion of not having an initialize in the subclasses but rather a "render" or "setup" that is called from the base classes initialize.... I'll have to talk it over with the team I'm working with so we can decide on how we want to do this. Anyway, thanks for the help and great advice. jonlb On Aug 20, 4:16 pm, nwhite <changereal...@gmail.com> wrote: > On a performance level, your initialize mutator will create a closure and > more function calls for every single class that gets initialized, not just > your desired classes. While I have in the past thought this syntax to be > elegant it can get confusing with other developers because of the 'magic' > nature, so it could introduce debugging issues. I prefer a more explicit > interface, I have found that if I need these sort of solutions I probably > need to take a step back and rethink my design pattern (not saying yours is > flawed). There is a lot of theory behind the Binds Mutator and why it is > flawed, I am probably not the one to explain it but in short it breaks > inheritance. > > It sounds like your subclasses follow a decorator pattern. Is this correct? > It might be helpful if you can explain your usage a bit. If I understand > correctly when you initialize a subclass you want it to execute its > initialization code then have the base class handle after that > initialization? > > SubClass = new Class({ > > Extends : BaseClass, > > initialize : function(){ > // do specific code here > // when done call base > this.parent(); > } > > }); > On Thu, Aug 20, 2009 at 3:36 PM, Jon Bomgardner <jo...@comcast.net> wrote: > > > I understand that it's a "hack". I guess for my own curiosity, why is > > it such a bad thing (other than it probably won't work in 2.0)? I > > would think that the ability to intercept calls to functions like that > > would be a good thing.... > > > On Aug 20, 3:30 pm, Aaron Newton <aa...@iminta.com> wrote: > > > To be clear, it's a hack. And it probably won't work in MooTools 2.0. I > > > probably shouldn't have mentioned it. > > > > On Thu, Aug 20, 2009 at 3:01 PM, Jon Bomgardner <jo...@comcast.net> > > wrote: > > > > > That could work though it would require rewriting all of the existing > > > > classes.... > > > > > Aaron, is there a reason why the approach you used in the Binds > > > > mutator wouldn't work in this case as well? perhaps something like > > > > this: > > > > > Class.Mutators.initialize = function(initialize){ > > > > return function(){ > > > > var result = initialize.apply(this, arguments); > > > > if ($defined(this.functionToCall)) { > > > > this.functionToCall(); > > > > } > > > > return result; > > > > }; > > > > }; > > > > > Thanks, > > > > jonlb > > > > > On Aug 20, 1:46 pm, nwhite <changereal...@gmail.com> wrote: > > > > > What if you made it so your extended classes don't have an initialize > > > > > method. Create another function like 'setup', 'render' or 'init'. In > > > > your > > > > > base class in your initialize method check for this method and > > execute. > > > > Then > > > > > your base class has full control of the subclass execution, return > > state. > > > > > > On Thu, Aug 20, 2009 at 1:16 PM, Jon Bomgardner <jo...@comcast.net> > > > > wrote: > > > > > > > What I'm trying to do is automate some code that should run for > > EVERY > > > > > > class in my class hierarchy. This code would be in the base class > > but > > > > > > should only be called once a sub-class is fully initialized. I > > can't > > > > > > just add it to the parent's initialize because that function could > > be > > > > > > called at any point in the sub-class' initialize and this code > > needs > > > > > > to always run at the end. If I go the event route then every class > > > > > > created would need to fire off the proper event and I wanted this > > to > > > > > > be as transparent as possible as I won't be the only person writing > > > > > > subclasses. > > > > > > > I hope that makes a bit more sense. In the meantime, I'll take a > > look > > > > > > at the Binds mutator... there might be something there I can use. > > > > > > > Thanks for the help, > > > > > > jonlb > > > > > > > On Aug 20, 1:00 pm, Rolf <plentyofr...@gmail.com> wrote: > > > > > > > Not really sure if I understand what you're after exactly, but > > why > > > > not > > > > > > > fire an event from one place that's picked up in the other class? > > > > > > > > On Aug 20, 9:34 pm, jonlb <jo...@comcast.net> wrote: > > > > > > > > > This may sound like a stupid question but here goes anyway..... > > > > > > > > > Is there a way to hook into the return of the Class initialize > > > > method? > > > > > > > > In other words, I have a piece of code that I want run after a > > > > class > > > > > > > > or (and here's the hard part) any of its subclasses is > > initialized. > > > > I > > > > > > > > know that every method is wrapped by the wrap() method but i > > wasn't > > > > > > > > sure if that was true of the initialize method as well. If it > > is, I > > > > > > > > should be able to reimplement the wrap method with a call to > > the > > > > > > > > function I want run just before the return, correct? > > > > > > > > > Any help would be appreciated. > > > > > > > > > Thanks, > > > > > > > > jonlb