That page specific code has a domready event which actually hooks up
your widgets and classes and stuff to elements on the page and gets
everything running. EG:
@@@ javascript
window.addEvent('domready',function(){
new SomethingWidget('div.somethingwidget');
});
@@@
This is all fine and good. It's probly what you'll want most of the
time. This is MooTools version of "find some elements, do some stuff".
Is there a better way?
Now that event delegation is available (as a plugin, in core in the
next major release), have you found any better patterns?
—Thomas Aylott / subtleGradient
I'm not really sure if this counts as a different pattern, but I
always try to pass just the "root" element of 'the elements' to the
constructor: say for a tab class it would be the container element.
All other required elements are defined as CSS selectors in the
options, "relative" to the root element (which can of course be
overridden when constructing the instance). Then in initialize I would
have something like:
var someRequiredElements = root.getElements(this.options.someRequiredSelector);
to find the elements.
I would imagine this would work well with event delegation, as events
can be then be delegated to the 'root' element.
my control. So, on domready I find the parent node of my stuff and add
some basic delegation code. If an element that matches the selector
for my widget is clicked, I instantiate a widget for that element at
that time. Since it's only happening once, it happens pretty quick.
> Is there a better way?
I've lost the point but if we had Class.create then we could do:
var myClass = new Class({ ... });
window.addEvent('domready', myClass.create.pass(['id',
{options:''}]));
@anutron, not sure why you have that Hash. You're reimplementing the
purpose of addEvent, I think.
On Mar 12, 9:03 am, daKmoR <daK...@...> wrote:
> I use a $require(path) function to load files if they are needed...
> (or use a php script to combine them all to one single javascript
> file)
>
> for layouts I'm starting to use a separate UI Class, so I don't need
> to care about it...
> for example (from my blog entry) I have an window and it gets resize
> and controls and so on....
> and now I want it to have rounded corners - so I can create one ui
> class that uses 4 divs - one on each corner to insert images...
> and I can create a ui-class that creates the background with canvas..
> or I can even create this little make 1000 divs rounded corner
> thing.... all different UIs I never have to touch my functional
> windows class...
>
> the right UI classes get automatically instantiated once you call UI
> (<theme>)...
>
> for more info see:http://www.delusionworld.com/mootools/general-mootools-ui/
window.addEvent('domready',function(){ new Foo('[foo=bar]'); });
// other code
window.addEvent('domready',function(){ new Bar('#bar'); });
// other code
window.addEvent('domready',function(){ new
Whatchajigger('.whatchajigger'); });
VS
window.addEvent('domready',function(){
new Foo('[foo=bar]');
new Bar('#bar');
new Whatchajigger('.whatchajigger');
});
nutron wrote:
> I could have it all inside a single, giant domready statement, but
> that has two issues for me: 1) I hate giant methods and 2) I can't add
> things to the init method later (just add more domready statements).
nope. for my page initialization, I prefer to use one and keep my initialization methods tidy. But as I pointed out, I like to keep my ajax partials with their own domready statements for their own initialization.
On Thu, Mar 12, 2009 at 12:01 PM, subtleGradient (via Nabble) <ml-user%2B55783-747403698@...> wrote:
Is there any reason why people should not use multiple domready events? EG:
— Thomas Aylott / SubtleGradient.com