I'm looking to create a javascript library that can be used in
combination with an Actionscript based project (similar to the
xmlhttprequest library). Ive looked through the various documentation
on how to create Actionscript libraries but did not see anything when
it comes to a Javascript library. Can someone point me in a direction
where I can find more information about this?
the ultimate source for this is "hidden" in the Jangaroo build process
documentation:
https://github.com/CoreMedia/jangaroo-tools/wiki/Maven-Build-Process
However, it is more a reference than a "how-to" documentation, so
better start with a good example.
"xmlhttprequest" is not the best example to start with, since its AS3
API is already contained in jangaroo-browser, not in the
"xmlhttprequest" module itself.
(The reason is that xmlhttprequest is actually a browser API, only the
implementation is so quirky in some browsers that you need this
JavaScript to make the browsers conform to the standard).
A better example is soundmanager2, it is here on github:
https://github.com/CoreMedia/jangaroo-libs/tree/master/soundmanager2
So you could clone "jangaroo-libs" and have a close look at the module
soundmanager2
Essentially, you need the obligatory Maven pom.xml, and two source
folders:
* src/main/joo-js holds the actualy JavaScript code of the framework,
preferably in a subfolder called like the framework, and in subfolder
"joo", a <framework>.module.js file that takes care of loading the
JavaScript file. Note that <framework> must be the same as the
<artifactId> in pom.xml!
* src/main/joo-api contains the "fake" ("native") ActionScript API of
classes, variables and functions defined in the JavaScript code. It is
never actually compiled into JavaScript code (as the JavaScript code
is already there!), but instead used as a reference when compiling
other code that uses the framework.
SoundManager2 is really ideal to understand this concept, as it only
consists of one global variable and three classes with a few methods.
The module is built by "mvn install". Then, you'd create a new
Jangaroo application module containing ActionScript code that uses the
JavaScript framework through its AS3 API. The pom.xml of this new
module needs to specify a Maven dependency on your framework module.
When building the application (again, "mvn install", but also "mvn
package" suffices), the Jangaroo compiler uses joo-api as reference
and copies the joo-js files into the resulting Web application.
Eventually, your compiled AS3 code as well as the JavaScript framework
code will be loaded automatically by jangaroo-application.js, which
(as for any Jangaroo application) has to be loaded by your HTML
wrapper (<script src="joo/jangaroo-application.js">).
Hope this helps for getting started, feel free to ask again if you get
stuck!
Greetings,
-Frank-
Brian
Here is my my apples.js Class.
(function () {
function Apples() {
};
Apples.prototype.getInfo= function() {
return 'test';
};
})()
and my Apples.as
package {
public class Apples
{
public native function getInfo():String;
public function myTest():void
{
}
}
}
my Main.as file
package com.acme.main {
import com.acme.HelloWorld;
public class Main {
public static function main(outputElementId:String):void {
var temp:Apples = new Apples();
trace(temp.getInfo());
Apples = (function() {
...
var Apples = function Apples() {
...
};
...
return Apples;
})();
Also, for JavaScript code, too, I'd recommend using packages:
joo.getOrCreatePackage("com.acme.example").Apples = (function()
{ ... })();
Did you put Apples.as under src/main/joo-api?
I do not see the native apples java script class referenced in the
generated js code. If the native js class is stored in the project
target, where would I find it?
Brian
File layout:
pom.xml
src\main\joo-api\soundManager.as
src\main\joo-api\com\schillmania\sm2\SMSound.as
src\main\joo-api\com\schillmania\sm2\SMSoundOptions.as
src\main\joo-api\com\schillmania\sm2\SoundManager.as
src\main\joo-js\joo\soundmanager2.module.js
src\main\joo-js\soundmanager2\script\soundmanager2-jsmin.js
src\main\joo-js\soundmanager2\script\soundmanager2-nodebug-jsmin.js
src\main\joo-js\soundmanager2\script\soundmanager2.js
From what you describe, it seems you are missing the
<artifactId>.module.js file, which references the "native" JavaScript
file via joo.loadScript("<relative-file-path-and-name>").
Im still having an issue however and I see the same issue when I try
and load the soundmanager2 module. When I build the application my js
classes get moved to the root of the target directory instead of the
joo/classes directory.
When I run this application
package com.acme.main {
import com.schillmania.sm2.SoundManager;
public class Main {
public static function main(outputElementId:String):void {
var temp:SoundManager = new SoundManager();
}
}
I get this error.
Jangaroo Runtime: Class com.schillmania.sm2.SoundManager not found at
URL [file:///C:/my-joo/my-joo-app/target/my-joo-app-0.1-SNAPSHOT/joo/
classes/com/schillmania/sm2/SoundManager.js].
Should the SoundManager.js file be located in "/joo/classes/com/
schillmania/sm2/"?
Nathan
target/my-joo-app-0.1-SNAPSHOT/joo/classes/com/schillmania/sm2/
SoundManager.js
indicates that the native implementation class written in pure
JavaScript has not been loaded properly.
I also tried to run a little application importing the soundmanager
module. Looking into target/my-joo-app-0.1-SNAPSHOT/joo, it turned
out that the soundmanager2.module.js file has not been copied
correctly into the jangaroo-application.js file. The corresponding
line in jangaroo-application.js reads:
// FROM net.jangaroo:com.schillmania.soundmanager2:0.9.6:
// FROM ...
where it should read:
// FROM net.jangaroo:com.schillmania.soundmanager2:0.9.6:
joo.loadScript("soundmanager2/script/soundmanager2-nodebug-jsmin.js",
"soundmanager2/script/soundmanager2.js");
//FROM: ....
Guess we need to investigate if that's a Jangaroo Maven plugin bug.
A quick fix is to load the soundmanager2.module.js yourself, after
the jangaroo-application.js, in your HTML file:
<script type="text/javascript" src="joo/jangaroo-application.js"></
script>
<script type="text/javascript" src="joo/soundmanager2.module.js"></
script>
Let me know if that works for you.
So make sure your module.js file is named correctly. Check whether the
code form it is included in jangaroo-application.js.
I just fixed the soundmanager2 module on github. If you want to try it
without waiting for the next release: clone jangaroo-libs, run 'mvn
install' to install the new jangaroo-libs snapshot version in your
local maven repository, and change the module dependency in your
application to
<dependency>
<groupId>net.jangaroo</groupId>
<artifactId>com.schillmania.soundmanager2</artifactId>
<version>0.9.7-SNAPSHOT</version>
<type>jangaroo</type>
</dependency>