PDA

View Full Version : Ext.onReady, namespaces, and scope



fernando
30 Aug 2007, 1:14 PM
Hi Guy's,

To help make my code more readable I'm trying to package my widgets up into namspaces. I want to then call each of these from a 'co-ordinator' namespace which in turn is called from Ext.onReady inside the html header.

So in the html file I would have:

<script type="text/javascript">
Ext.onReady(coOrdinator.start.init, coOrdinator.start);
</script>

the co-ordinator would then be something like:


coOrdinator.app = function() {
return {
init : function() {
treeNameSpace.app.init_tree;
menuNameSpace.app.init_menu;
}
};
}();

The tree and menu files would then look like:

Ext.namespace('treeNameSpace');

treeNameSpace.app = function() {

return {

init_tree : function(){
//tree code here
}

However I'm having trouble with scope. I have been through the tutorials but I'm still not able to make this work.
What scope should the Ext.onReady be set to?

any suggestions would be much appreciated.

Tim

brian.moeskau
30 Aug 2007, 1:20 PM
Maybe this is a typo in your post, but you have defined a method called coOrdinator.app.init, but you are calling the method coOrdinator.start.init. If you change the call to app instead of start does it work?

fernando
30 Aug 2007, 1:31 PM
Really sorry that was a typo.

Am I doing something dumb here?

Does anyone else use a similar method for packaging everything up?

brian.moeskau
30 Aug 2007, 1:38 PM
Most people do not create separate namespaces for each component. Really, you probably only need a single namespace for your company or application name, then all your application modules would go under that namespace.

I think your scope problem may be that you're not passing 'true' as the third param to Ext.onReady. See http://extjs.com/deploy/ext/docs/output/Ext.html#onReady

Of course, you also seem to have this code spread out over multiple areas since your onReady block is in its own script tag. If you can't get it to work, post your entire page so we can see the full context.

fernando
30 Aug 2007, 1:58 PM
OK thanks for the advice.
I've moved the Ext.onReady into the coOrdinator and I now only have one namespace.
And I'm up and running.

Many thanks.