Discussion:
[qooxdoo-devel] Loading a class by name
Joachim Baran
2010-04-13 18:24:54 UTC
Permalink
Hi!

Is it possible to instantiate an object just by its classname?

I would like to dynamically load classes into my application that are
given under "build-script"/"settings" in config.json. Right now, I am
trying to see of the class is available in general by calling
'qx.Class.isDefined("test.Contribution");'. Surprisingly (to me) the
method returns 'false' unless I explicitly create an object with 'new
test.Contribution();' first. Of course, I would like to avoid that,
because I want do not want to modify my application and instead only
"plug-in" objects by just giving their classnames.

Thanks,
Joachim
--
B.1079 Michael Smith Building
Faculty of Life Sciences
The University of Manchester
Oxford Road
Manchester
M13 9PT
United Kingdom
Florian Giesen
2010-04-14 07:01:11 UTC
Permalink
Hello Joachim!

Did you try this with the source or build version of your qooxdoo
application? In the buid version only classes are loaded which are used by
your script (I don't know, how the source version behaves. Maybe you can use
a generator option like --source-all. See the "Generator" section in
the qooxdoo
manual <http://qooxdoo.org/documentation/1.0>). IMHO this is one of the main
optimisations the generator performs.

You should have a look at the package loading system. With this you can load
packages (and the included classes) on demand at runtime (
http://qooxdoo.org/documentation/1.0/parts_using).


Regards, Florian
Post by Joachim Baran
Hi!
Is it possible to instantiate an object just by its classname?
I would like to dynamically load classes into my application that are
given under "build-script"/"settings" in config.json. Right now, I am
trying to see of the class is available in general by calling
'qx.Class.isDefined("test.Contribution");'. Surprisingly (to me) the
method returns 'false' unless I explicitly create an object with 'new
test.Contribution();' first. Of course, I would like to avoid that,
because I want do not want to modify my application and instead only
"plug-in" objects by just giving their classnames.
Thanks,
Joachim
--
B.1079 Michael Smith Building
Faculty of Life Sciences
The University of Manchester
Oxford Road
Manchester
M13 9PT
United Kingdom
------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
qooxdoo-devel mailing list
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Alexander Steitz
2010-04-14 07:55:28 UTC
Permalink
Hi Joachmin,
Post by Joachim Baran
Is it possible to instantiate an object just by its classname?
I would like to dynamically load classes into my application that are
given under "build-script"/"settings" in config.json. Right now, I am
trying to see of the class is available in general by calling
'qx.Class.isDefined("test.Contribution");'. Surprisingly (to me) the
method returns 'false' unless I explicitly create an object with 'new
test.Contribution();' first. Of course, I would like to avoid that,
because I want do not want to modify my application and instead only
"plug-in" objects by just giving their classnames.
The generator need to have knowledge about the classes you want to use. If the
necessary classes can't be obtained by analyzing the source code you have to
add these classes manually so the generator knows them.

You can do this by either adding an "include" block into a job in your
config.json or you place a "#require" hint at the source code files.

--snip--
/* ******************************************
#require(namespace.class)
****************************************** */
--snip--

This way the generator resolves these classes and you can use them at runtime.

cheers,
Alex
Joachim Baran
2010-04-14 14:38:55 UTC
Permalink
Hi,

Thanks Alexander and Florian, but unfortunately I cannot get it to work.
Post by Alexander Steitz
#require(namespace.class)
I have tried #require(mylib.Contribution) in both the application's
class as well as the contribution's class without any luck.

I then modified my config.json like this:
"jobs" :
{
"source" :
{
"require" :
{
"myapp.Application" : [ "mylib.Contribution" ]
}
}
}
Unfortunately, this did not work either.

The manual on qooxdoo.org is a bit sparse on this issue, so perhaps
you could give me another hint how to fix this?

The actual code, qx.Class.isDefined("mylib.Contribution"), is called
in the main function of myapp.Application, which I compile with
'build'.

Thanks for the help,
Joachim
--
B.1079 Michael Smith Building
Faculty of Life Sciences
The University of Manchester
Oxford Road
Manchester
M13 9PT
United Kingdom
Werner Thie
2010-04-14 14:49:51 UTC
Permalink
Hi

Digging through the source made me aware of the #use statement.

So, use like this for fine tuning availability of whatever code in
source and build mode in your Application.js or deeper down the
hierarchy file:

#use(qx.ui.root.Inline)
#use(qx.ui.layout.VBox)
#use(qx.ui.form.Button)
#use(qx.fx.effect.core.Move)

Works like a charm!

HTH, Werner
Post by Joachim Baran
Hi,
Thanks Alexander and Florian, but unfortunately I cannot get it to work.
Post by Alexander Steitz
#require(namespace.class)
I have tried #require(mylib.Contribution) in both the application's
class as well as the contribution's class without any luck.
{
{
{
"myapp.Application" : [ "mylib.Contribution" ]
}
}
}
Unfortunately, this did not work either.
The manual on qooxdoo.org is a bit sparse on this issue, so perhaps
you could give me another hint how to fix this?
The actual code, qx.Class.isDefined("mylib.Contribution"), is called
in the main function of myapp.Application, which I compile with
'build'.
Thanks for the help,
Joachim
Alexander Steitz
2010-04-14 15:04:32 UTC
Permalink
Hi Joachim,
Post by Joachim Baran
I have tried #require(mylib.Contribution) in both the application's
class as well as the contribution's class without any luck.
{
{
{
"myapp.Application" : [ "mylib.Contribution" ]
}
}
}
Unfortunately, this did not work either.
The manual on qooxdoo.org is a bit sparse on this issue, so perhaps
you could give me another hint how to fix this?
The actual code, qx.Class.isDefined("mylib.Contribution"), is called
in the main function of myapp.Application, which I compile with
'build'.
Do you added the "mylib.*" classes as a library in your applications
config.json file? For the generator every top-level namespace is represented by
a library and this library has to be inlcuded with the "libraries" key into
your application configuration.

Can you please post your "config.json" so we can take a look at it?

cheers,
Alex
Joachim Baran
2010-04-14 15:21:24 UTC
Permalink
Hello,
Post by Alexander Steitz
Can you please post your "config.json" so we can take a look at it?
The whole thing? Oh well.. here it comes:
{
"name" : "Gazebo",

"include" :
[
{
"path" : "${QOOXDOO_PATH}/tool/data/config/application.json"
}
],

"export" :
[
"api",
"build",
"clean",
"distclean",
"fix",
"info",
"inspector",
"lint",
"migration",
"pretty",
"profiling",
"source",
"source-all",
"test",
"test-source",
"translation"
],

"let" :
{
"APPLICATION" : "gazebo",
"QOOXDOO_PATH" : "../support/qooxdoo",
"QXTHEME" : "gazebo.theme.Theme",
"API_EXCLUDE" : ["qx.test.*", "${APPLICATION}.theme.*",
"${APPLICATION}.test.*"],
"LOCALES" : [ "en" ],
"CACHE" : "${TMPDIR}/cache",
"ROOT" : "."
},

"jobs" :
{
"libraries" :
{
"library" :
[
{
"manifest" : "../../Fly/fly/trunk/Manifest.json"
}
]
},

"build-script" :
{
"settings" :
{
"gazebo.contributions" : [ "fly.Contribution" ]
}
},

"source" :
{
"require" :
{
"gazebo.Application" : [ "fly.Contribution" ]
}
}
}

}

Gazebo is the name of my application that I am building and the
contribution 'fly.Contribution' is supposed to be a plug-in that
extends Gazebo (it was created via 'create-application.py -t
contribution -n fly').

Thanks,
Joachim
--
B.1079 Michael Smith Building
Faculty of Life Sciences
The University of Manchester
Oxford Road
Manchester
M13 9PT
United Kingdom
Thomas Herchenroeder
2010-04-14 16:26:08 UTC
Permalink
Post by Joachim Baran
{
{
"gazebo.contributions" : [ "fly.Contribution" ]
}
},
{
{
"gazebo.Application" : [ "fly.Contribution" ]
}
Well, these two are not the same, right?! For your source version you
require a class named "fly.Contribution", but in the build version you
don't. On the other hand, for the build version you set a setting
("gazebo.contributions"), but in the source version you don't.

For one thing, this explains why your build version doesn't contain
fly.Contribution.

T.
Joachim Baran
2010-04-14 16:47:53 UTC
Permalink
On 14 April 2010 17:26, Thomas Herchenroeder
Post by Thomas Herchenroeder
Well, these two are not the same, right?!
For one thing, this explains why your build version doesn't contain
fly.Contribution.
Oh my.. you are right! I always mess up these things up. Sorry about that..

However, I have now everything under "build" and it works when I say:
"require" : { "gazebo.Application" : [ "gazebo.test.DemoTest" ] }
but when I put there
"require" : { "gazebo.Application" : [ "fly.Contribution" ] }
I see the following error messages in the JavaScript console:
Uncaught ReferenceError: fly is not defined
Uncaught TypeError: Object qx.event.type.Event[4] has no method
'getReturnValue'

How do I make 'fly' known to qooxdoo? When I compile with -v, it tells
me "Detected namespace: fly", but I assume that does not carry over to
runtime?

Joachim
--
B.1079 Michael Smith Building
Faculty of Life Sciences
The University of Manchester
Oxford Road
Manchester
M13 9PT
United Kingdom
Joachim Baran
2010-04-14 20:32:05 UTC
Permalink
It works now.

I changed 'fly.Contribution' to 'gazebo.fly.Contribution' and now the
class is found.

Is there some documentation on how I can create an instance of said
class from its Class object though?

Thanks a lot,
Joachim
--
B.1079 Michael Smith Building
Faculty of Life Sciences
The University of Manchester
Oxford Road
Manchester
M13 9PT
United Kingdom
thron7
2010-04-14 15:28:46 UTC
Permalink
Post by Joachim Baran
Post by Alexander Steitz
#require(namespace.class)
I have tried #require(mylib.Contribution) in both the application's
class as well as the contribution's class without any luck.
{
{
{
"myapp.Application" : [ "mylib.Contribution" ]
}
}
}
I suggest to take one step at a time :). So try to get this dynamical
thing running within a single application, and without adding additional
libraries.

Secondly, both methods, using #require or the "require" config key, are
equivalent. #use, suggested elsewhere, does about the same, but does not
influence the load order. For your purpose, all three should work.

Thirdly, it should work in both source and build versions, so you can
choose which version you want to look at; 'source' might actually be
better for other reasons.

Fourthly, you can check the availability of a class by either adding
'-v' to the generate.py invocation and check that the desired class is
in the (long) list of included classes printed to the console. Or, in
the browser open a console, like Firebug, and enter the class name,
which should return something sensible (like an object ref).

If you still fail, then yes, send in the config.json of your app.

T.
Werner Thie
2010-04-14 15:45:23 UTC
Permalink
Hi

on an other note - I spent the last two days with a nasty problem
occurring with WebKit. If using JS in a multifile situation you can
easily observe that WebKit (Safari, Chrome) does NOT in order loading of
given JS files, meaning you end up in a situation where JS code might
sometimes run and sometimes fail dependent on server/local situations. I
don't know where the WebKit guys got this crazy idea that evaluating JS
source out of order might speed up things, but they clearly did it that
way which is in my opinion violating the specs.

Werner
Post by thron7
Post by Joachim Baran
Post by Alexander Steitz
#require(namespace.class)
I have tried #require(mylib.Contribution) in both the application's
class as well as the contribution's class without any luck.
{
{
{
"myapp.Application" : [ "mylib.Contribution" ]
}
}
}
I suggest to take one step at a time :). So try to get this dynamical
thing running within a single application, and without adding additional
libraries.
Secondly, both methods, using #require or the "require" config key, are
equivalent. #use, suggested elsewhere, does about the same, but does not
influence the load order. For your purpose, all three should work.
Thirdly, it should work in both source and build versions, so you can
choose which version you want to look at; 'source' might actually be
better for other reasons.
Fourthly, you can check the availability of a class by either adding
'-v' to the generate.py invocation and check that the desired class is
in the (long) list of included classes printed to the console. Or, in
the browser open a console, like Firebug, and enter the class name,
which should return something sensible (like an object ref).
If you still fail, then yes, send in the config.json of your app.
T.
------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
qooxdoo-devel mailing list
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
thron7
2010-04-14 16:22:19 UTC
Permalink
Werner,

could you re-post this in its own thread?! It's worth it, and this makes
it easier to keep topics on focus.

Thanks,
T.
Post by Werner Thie
Hi
on an other note - I spent the last two days with a nasty problem
occurring with WebKit. If using JS in a multifile situation you can
easily observe that WebKit (Safari, Chrome) does NOT in order loading of
given JS files, meaning you end up in a situation where JS code might
sometimes run and sometimes fail dependent on server/local situations. I
don't know where the WebKit guys got this crazy idea that evaluating JS
source out of order might speed up things, but they clearly did it that
way which is in my opinion violating the specs.
Werner
Post by thron7
Post by Joachim Baran
Post by Alexander Steitz
#require(namespace.class)
I have tried #require(mylib.Contribution) in both the application's
class as well as the contribution's class without any luck.
{
{
{
"myapp.Application" : [ "mylib.Contribution" ]
}
}
}
I suggest to take one step at a time :). So try to get this dynamical
thing running within a single application, and without adding additional
libraries.
Secondly, both methods, using #require or the "require" config key, are
equivalent. #use, suggested elsewhere, does about the same, but does not
influence the load order. For your purpose, all three should work.
Thirdly, it should work in both source and build versions, so you can
choose which version you want to look at; 'source' might actually be
better for other reasons.
Fourthly, you can check the availability of a class by either adding
'-v' to the generate.py invocation and check that the desired class is
in the (long) list of included classes printed to the console. Or, in
the browser open a console, like Firebug, and enter the class name,
which should return something sensible (like an object ref).
If you still fail, then yes, send in the config.json of your app.
T.
------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
qooxdoo-devel mailing list
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
qooxdoo-devel mailing list
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Werner Thie
2010-04-14 16:52:02 UTC
Permalink
Hi

I spent the last two days with a nasty problem
occurring with WebKit. If using JS in a multifile situation you can
easily observe (just put a debugger; or alert statement at the beginning
of the file) that WebKit (Safari, Chrome) does NOT in order loading of
given JS files, meaning you end up in a situation where JS code might
sometimes run and sometimes fail dependent on server/local situations. I
don't know where the WebKit guys got this crazy idea that evaluating JS
source out of order might speed up things, but they clearly did it that
way which is in my opinion violating the specs.

Solving the problem means you have to introduce a system of guaranteeing
that code you depend on is loaded when the file in question is evaluated
(see code below)

Werner

Assuming that a file yyy.js containing JS code injects a top level
object with the filename yyy and is dependent on xxx.js:

//// code goes someplace early, like Application.js
modulesWaiting = {};

function moduleLoaded(module) {
modulesWaiting[module] = null;
}

function waitForModules(module, f) {
modulesWaiting[module] = f;
for (var m in modulesWaiting) {
if (modulesWaiting[m])
modulesWaiting[m]();
}
}

//// code of a single file yyy.js dependant on code in file xxx.js
waitForModules('yyy', function() {
var deps = ['xxx']; //comma separated list of modules
var loaded = true;
for (var i=0; i<deps.length; i++)
loaded = loaded && (deps[i] != null);
if (loaded) {
//here comes the source, Luke
//your source code

yyy = {}; //signal readyness by defining this object
moduleLoaded('yyy');
}
});
thron7
2010-04-14 17:30:24 UTC
Permalink
Werner, thanks for making the effort with the changed subject. But for
most mail clients this will not suffice as they are tracking message
id's, and these are retained when you *reply* to an existing post. To
really start a new thread you have to write a fresh message to the
mailing list. Will you give it another try?!

T.
Post by Werner Thie
Hi
I spent the last two days with a nasty problem
occurring with WebKit. If using JS in a multifile situation you can
easily observe (just put a debugger; or alert statement at the beginning
of the file) that WebKit (Safari, Chrome) does NOT in order loading of
given JS files, meaning you end up in a situation where JS code might
sometimes run and sometimes fail dependent on server/local situations. I
don't know where the WebKit guys got this crazy idea that evaluating JS
source out of order might speed up things, but they clearly did it that
way which is in my opinion violating the specs.
Solving the problem means you have to introduce a system of guaranteeing
that code you depend on is loaded when the file in question is evaluated
(see code below)
Werner
Assuming that a file yyy.js containing JS code injects a top level
//// code goes someplace early, like Application.js
modulesWaiting = {};
function moduleLoaded(module) {
modulesWaiting[module] = null;
}
function waitForModules(module, f) {
modulesWaiting[module] = f;
for (var m in modulesWaiting) {
if (modulesWaiting[m])
modulesWaiting[m]();
}
}
//// code of a single file yyy.js dependant on code in file xxx.js
waitForModules('yyy', function() {
var deps = ['xxx']; //comma separated list of modules
var loaded = true;
for (var i=0; i<deps.length; i++)
loaded = loaded && (deps[i] != null);
if (loaded) {
//here comes the source, Luke
//your source code
yyy = {}; //signal readyness by defining this object
moduleLoaded('yyy');
}
});
------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
qooxdoo-devel mailing list
https://lists.sourceforge.net/lists/listinfo/qooxdoo-devel
Loading...