GWTTestcase accessing javascript object defined in an external javascript file fails - gwt

I have defined a GWT module that includes an external javascript file using tag. I have written a GWTTestCase that returns the above described module's name. When my testcase accesses a javascript object I see the following exception
Caused by: com.google.gwt.core.client.JavaScriptException: (null): null
Any idea on how to fix this?
Am I right in assuming that the scripts included in the gwt module definition file will be available when executing the GWTTestCase?

I have fixed it myself. Apparently, when accessing such objects it should be referenced using $wnd variable.
Example: test.js defined object test. In order to access it from GWT one should use, $wnd.test
Hope this answers saves somebody else' time.

Related

"Modules that use an anonymous define() call must be loaded with a require() call"

In the walkthrough step 7: JSON Model example, the app apparently works as documented but I see the following error in the console:
Error: Modules that use an anonymous define() call must be loaded with a require() call; they must not be executed via script tag or nested into other modules.
The only other instance of this message that I could find seems, to my untrained eye, to deal with a wholly different scenario.
I've tried both Firefox and Chromium, CDN hosting vs local hosting, two different UI5 versions (1.77.0 and 1.79.0), both minified and plain, so I'd suppose this is really something in the code itself.
What could it be? Also, is it something I can safely ignore and why?
Anonymous define
Calling sap.ui.define([...],...) defines a module anonymously because the 1st argument is not a string (module name) but a list of the module's dependencies. If the module name is omitted, the framework automatically determines it based on how the module script was referenced.
Use anonymous sap.ui.define once at top-level of the JS file content, not multiple times.
Replace sap.ui.define with sap.ui.require when simply requiring existing modules.
Cf. my comment at https://github.com/SAP/openui5/issues/2203#issuecomment-420918457.
Named module define
The 1st argument in sap.ui.define("MyModule",[...] ,...) defines the name of the module manually which must be passed when:
Defining a nested module within an existing module definition in a single JS file content.
Defining a module which was initiated by a <script> tag from HTML.
The walkthrough is fixed with SAP/openui5#6302b8f and SAP/openui5-docs#43 accordingly.

Compiling/Joining CoffeeScript files in correct order

Correct me if I am wrong, but it appears that CoffeeScript does not compile/join code (each class has its own file) in the correct order.
If I have these classes in the following files:
Button.coffee
class Button extends UIComponent
UIComponent.coffee
class UIComponpent
When I compile these classes (using the --join flag), it outputs the classes in the incorrect order (i.e. putting Button ahead of UIComponent). So when the referenced .js file is used on a web page, it throws the "Cannot read property 'prototype' of undefined" error
Is this an issue that anyone else experiences? If so, is the standard use of CoffeeScript to not use classes? I'm just confused on why this doesn't appear to be a standard implementation? Perhaps I am using CoffeeScript incorrectly.
CoffeeScript isn't responsible for your dependency management.
You could use something like require.js to define your dependencies, then use CoffeeScript to compile your JavaScript files separately, and then use the r.js optimiser to minify and concatenate your compiled JS.

Fay: include another Fay file?

I have one Fay file which is the heart of my program, however I need some helpers for my logic, for instance a method to replace substrings. From what I understand, if I need such methods which are offered by many Haskell libraries from Hackage directly, I can't use those Haskell libraries, but I must copy-paste the code in my project. So it's what I did, I copy-pasted a "replace" function together with other helpers from the MissingH library in a new file in my project: Utils.hs.
That Utils.hs compiles without problems with Fay. Also I import it in my main Fay file and I get a JS file for the main project file without problems. However at runtime I get the following error:
ReferenceError: Utils$$36$ is not defined
I don't think that Fay will include the code from the helper file in my main JS file, so I'm including both JS files in the loading HTML. And to make even more sure that when I load the main file, that the utils file is loaded, I load it like that:
$.getScript("Utils.js", function(){
$.getScript("FayConfig.js");
});
But despite this I still get the error. I tried compiling the Utils.hs with "--library" but it didn't help.
So my question is, which setup do I need to achieve that the generated JS will find the helper functions that I put in another HS file, knowing that at compile-time, Fay (apparently) finds them without problems? Is there an example of such a setup online? Most of the Fay uses that I found have all the code in a single HS file, though they often use external Fay code from cabal, as with fay-jquery. In my case, setting up a cabal project just for these simple helpers would be overkill.
Which version of Fay are you using (fay --version)? It seems like you are using a version older than
0.16 where forgetting import Prelude wouldn't give any warnings, see this closed ticket. So upgrade fay and/or add import Prelude.
We're also considering renaming operators in the produced output to make error messages like these easier to understand.
You do not need to invoke fay several times, fay outputs all dependencies into the same js file. So there's no difference from using a cabal package in that regard.
Hope this helps, otherwise please give me a way to reproduce this.

Are there special rules to follow when putting splitting coffeescript classes into separate files?

I'm trying to change some otherwise working code by pulling all the classes into separate files. This works for most classes, except for the part where it reads class window.Timeline. The error message reads ReferenceError: window is not defined
Any suggestions?
It sounds like your file containing that class isn't getting loaded into the window context. Is it possible that it got loaded in the context of another class? Could you post some cod examples in a jsFiddle?
The pattern that I usually follow when exporting coffeescript symbols to the parent context is
exports = exports ? this
class MyClass
someField: false
exports.MyClass = MyClass
If you are using a modern browser and know how to access the debugging console, you could put
console.log this
At the end of the file that is throwing the reference error. This will allow you to have a look to see what the this context is, which may help you troubleshoot.

GWT and ReflectionToStringBuilder.toString()

I have a problem with the GWT compiler. When I add a next method to my entity class:
#Override
public String toString() {
return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
}
the compiler gave me next error:
ERROR: Deferred binding failed for 'com.mvp4g.client.Mvp4gModule'; expect subsequent failures
ERROR: Unable to load module entry point class plaut.wimc.avl.admin.client.Admin (see associated exception for details)
java.lang.RuntimeException: Deferred binding failed for 'com.mvp4g.client.Mvp4gModule' (did you forget to inherit a required module?)
When I remove it, all works fine. I don't understand why gave me compiler such error. This toString method is used in roo IDT's too and there is no such error.
All Java code used in the client side needs to be able to compile to JavaScript. ReflectionToStringBuilder uses reflection which isn't available in JavaScript so this method can't be used in your client side code.
This compile error refers to the fact that all Java code must be accessed by the GWT copmiler via path parameters in GWT module files and must be available in source format. In this case no GWT module file is present, hence the error, because the compiler can't find the sources for the ReflectionToStringBuilder method. Although you can create such a file for this specific case and add the sources, it won't work as reflection won't work.
Looks like the problem is with ReflectionToStringBuilder. Is it a GWT module? If yes, then it needs to be added an inherited module in your project's *.gwt.xml