Passing functions is not usually what one does, you _can_ pass a callback
that signals the parent class to do something else but you usually let your
children or composed classes do what they need to do.
Kind of like this:
Parent.askChildrenForTheirReportCards(); ==
Parent.getChildren().map(function(child){ return child.getReportCard(); });
If the action getReportCard is heavy, or async you would have this kind of
API:
Parent.askChildrenToWorkOnHomework(); ==
Parent.getChildren().each(function(child){
child.workOnHomeWork(this.onChildFinishedHomework); }, *this)*;
Notice that I passed *, this)* to the Array.each method. That's how you
bind another function to another context (the parent's context, or this).
You can also do that with .pass(['first argument', 'second argument'], *this
*); Take a look at the documentation.
So again, you don't pass around functions for the children to do. You
usually define that function in the child (it's as if the child needed to
be told exactly what to do by the parent -- it wouldn't be fun and it'd be
condescending).
--
If I may comment on your code. You should use real words in your variables
and method names.
var b to var tab;
var ff = who knows what.
getassetG isn't using the proper camelCase.
and so on.
Here's a style guide that might help:
https://github.com/mootools/mootools-core/wiki/syntax-and-coding-style-conventions
Here's another:
http://google-styleguide.googlecode.com/svn/trunk/javascriptguide.xml
It's important for you, and for *us* that you have a consistent style.
On Fri, Dec 2, 2011 at 10:12 AM, davepilbeam <[email protected]>wrote:
> Hi everyone,
>
> I split my (working fine) UI widget up into separate Classes for
> reusability. Some of these Classes load on demand but still need to
> pass info on to their sibling Classes.
>
> I ended up with:
> a Tab Class containing a Tree Class that asks for a JS Asset that
> loads an Uploader Class that on completion refers back to the Tab
> Class
>
> But .bind or no .bind, by the end of my increasingly desperate 'pass
> the parcel' nested functions, I've still lost my 'this'
>
> What's the best way to properly pass functions across Class to Class
> like this, without storing stuff in global variables?
>
> Simple version: http://jsfiddle.net/davepilbeam/YeF8K/
>
> Thanks in advance
>