Maybe my question is simple to solve for you, but I'm sitting here for a few hours with following problem (and I have already searched for it):
I have a Java method which has JavaScript code inside (JSNI from Google's GWT) to set key bindings for the code editor CodeMirror2. When I want to compile with GWT, I'm getting an error from the GWT compiler:
[ERROR] Line 195: invalid label
> "F11": function() {
My code looks like this:
public final native void setExtraKeysCallback() /*-{
this.extraKeys = function(editor) {
"F11": function() {
// do something
},
"Esc": function() {
// do something
}
};
}-*/;
The really strange thing is that in native JS the extra keys are working with this syntax?! I think the problem is the label syntax, how could I change this to work with GWT?
Thanks, leX
You seem to be mixing object literal syntax:
{ "key": value, "key2", value2 }
… with function expression syntax:
function () { }
Decide if you are trying to create a simple object or a function (or a function that returns a simple object) and use the appropriate syntax for that.
Related
To the downvoters - this is a legitimate question. Please take the time to examine it before assuming I'm mixing up my languages like some kind of programming newb!
I need to know if it's possible to import a Java object (specifically, an enum class) in a Typescript script.
I've googled but haven't found anything.
The ErrorCodeAuthority is for having custom, standardized errors thrown from our service for each known error with set messages (some parameterized, some not), http status codes, etc defined in one place.
In our javascript code we have
var JavaErrorCodeAuthority = Java.type("com.domain.ErrorCodeAuthority");
Is it possible to do the same in Typescript?
Edit based on answer below
I've declared the following:
declare module Java {
export enum ErrorCodeAuthority {
ENTITY_NOT_FOUND,
HTTP_VERB_NOT_SUPPORTED,
BAD_REQUEST,
//...
}
export function type(arg: "com.domain.ErrorCodeAuthority"): ErrorCodeAuthority;
export function type(arg: string): any;
}
var JavaErrorCodeAuthority = Java.type("com.domain.ErrorCodeAuthority");
and I'm attempting to use the new type as follows:
export class HttpFailResult extends HttpResult {
constructor(public errorCode : Java.ErrorCodeAuthority, public userParams? : UserParam[]) {
super(errorCode.httpStatus.value(), errorCode.toString());
}
}
I'm getting the following error when I try to use grunt to compile to js:
error TS2339: Property 'httpStatus' does not exist on type 'ErrorCodeAuthority'.
(For reference, the super HttpResult is an object that contains a number http code and astringbody. HttpStatus, in the Java enum, is of typeorg.springframework.http.HttpStatus`).
I tried removing the export function type(arg: "com.domain.ErrorCodeAuthority"): ErrorCodeAuthority; line but that didn't change the exception.
EDIT 2
We're running all of this inside a nashorn container if that makes a difference
Is it possible to do the same in Typescript?
Yes. With 1c, you can just write
let JavaErrorCodeAuthority = com.domain.ErrorCodeAuthority
And there will be auto-completion on each level of packages.
Yes, if you already did this in JavaScript you can use the code by creating a definition file for it and port it to TypeScript.
An example might be like this:
declare module Java {
export enum ErrorCodeAuthority {
item1,
item2
}
export function type(arg: "com.domain.ErrorCodeAuthority"): ErrorCodeAuthority;
export function type(arg: string): any;
}
var JavaErrorCodeAuthority = Java.type("com.domain.ErrorCodeAuthority");
The enum and the first type function with the "com.domain.ErrorCodeAuthority" is optional but it gives you better typeinfo when passed in that particular string. Note the declare module part doesn't generate any code and you can add it to a .ts or .d.ts file. More info about creating a definition file can be found here: https://github.com/Microsoft/TypeScript/wiki/Writing%20Definition%20Files
EDIT
after the info from the comments I hope this code below will better suite your need.
This has the downside that it isn't usable in a switch statement but in this case I think it is better to see the java enum as a module (or class was possible to). This might not be 100% correctly modelled but hopefully it gives you some extra idea's. Just a small side note, I find your case very interesting and challenging!
declare module Java {
interface ErrorCodeValue {
toString(): string;
value(): number;
}
module ErrorCodeAuthority {
var ENTITY_NOT_FOUND: IErrorCodeAuthority;
var HTTP_VERB_NOT_SUPPORTED: IErrorCodeAuthority;
var BAD_REQUEST: IErrorCodeAuthority;
}
interface IErrorCodeAuthority {
httpStatus: ErrorCodeValue;
}
export function type(arg: "com.domain.ErrorCodeAuthority"): typeof ErrorCodeAuthority;
export function type(arg: string): any;
}
export class HttpResult {
constructor(code: number, description: string) {
}
}
export class HttpFailResult extends HttpResult {
constructor(public errorCode: Java.IErrorCodeAuthority, public userParams? :any[]) {
super(errorCode.httpStatus.value(), errorCode.toString());
}
}
var JavaErrorCodeAuthority = Java.type("com.domain.ErrorCodeAuthority");
new HttpFailResult(JavaErrorCodeAuthority.BAD_REQUEST, null);
Apologies if this is a bit of a weird one...
I have a program written in Java which utilises the ScriptEngine to process user provided JavaScript to extend my application. However, this specific question is related to general JavaScript as opposed to Java or it's ScriptEngine, but I am just explaining this to set the context.
I have a function which returns a string when called - let's call it a() as defined below:
var a = function() {
return "this is a";
};
When the user calls this function using a() it works fine and outputs "this is a". However, if the user forgets to include the parenthesis then it outputs my actual function definition - expecting this as I am no longer calling the function.
To catch this I have redefined the toString method of my Object to the following:
a.toString = function() {
return a();
};
This works fine when I use a in a string context as it calls the toString method implicitly, but if I attempt to pass it to a function then it doesn't call toString and I am left with a sun.org.mozilla.javascript.internal.InterpretedFunction.
I have looked at the other Function.prototype methods (i.e. apply, bind, constructor, etc) to try and override the method which is called as the function is passed to another function but none of them fitted the bill. I am basically looking for a way of converting a Function to a string type object whenever it is used without the parenthesis - i.e a === a(). For people who might ask why don't I define a as a string to start with, my function returns a string constructed from other information the user has provided.
Maybe the solution is to make my users write syntactically correct JavaScript, but my users are far from programmers. I could also add some form of pre-parsing which checks for missing parenthesis and adds them in dynamically before I execute it using the ScriptEngine. However, although both of these options will work, I am looking for an easier way.
Neither a.toString nor a.prototype.toString will allow you to forget the parenthesis. .toString allow you to do:
var a = function() {
return "this is a";
};
a.prototype.toString = function () {
return "something";
};
var A = new a;
alert(A + ''); // something
A.toString = function () {
return "something else";
};
alert(A + ''); // something else
You shouldn't want in your code both a() and a return the same thing, this looks like a very bad idea.
An option to get myObject.myVar return a custom dynamic string is defineGetter https://developer.mozilla.org/fr/docs/Web/JavaScript/Reference/Objets_globaux/Object/defineGetter
I have asked for this severals weeks ago, but nobody couldn't help me.
This is very important thing to me, and it's still without any working solution.
In ViewDwr I have simple method which are check some data by given Id and return state as boolean. In javascript (jsp file) I have something like that:
ViewDwr.checkData(id, function(data)
{
document.getElementById("status").innerHTML=data;
});
and it's working good. Problem is that I need to put data to variable. Something like:
var currentDataStatus;
ViewDwr.checkData(id, function(data)
{
currentDataStatus=data;
});
alert(currentDataStatus);
but in this case it isn't working. I trying any combination with that but i could't find any working solution to put resposne value to variable and using it in other functions.
Anybody could help me?
Thanks
This won't work because it's asynchronous processing. Your function will be called later on, when the results come back from the server. At this point, your context is long gone.
When programming asynchronously (like wih DWR) you have to abstract from the specific order of processing. Think in callbacks (OK, I won' start about promises etc. here). Like, "here's what should happen when the results come back".
In your second sample this will be just something like:
ViewDwr.checkData(id, function(data)
{
alert(data);
});
Why do you need a variable anyway? If this is meant to be a global variable this is not a good idea anyway. Otherwise consider making it a property in some object.
var myObject = {};
ViewDwr.checkData(id, function(data)
{
myObject.currentDataStatus = data;
alert(myObject.currentDataStatus);
});
Or even better, make your object more intelligent:
var myObject = ...;
ViewDwr.checkData(id, function(data)
{
myObject.onDataChecked(data);
});
Important is that you have to do the processing of the results (like alert(currentDataStatus)) in the success callback.
I am embedding some javascript in a Java application using Rhino. I am following the example on the Rhino website, executing a script by calling the Context's evaluateString method and passing the actual script in as a String.
I have a whole bunch of existing javascript code that I would like to make use of. I don't want to concatenate it all into an enormous String and pass it in to evaluateString. I would rather be able to load the code in so that I can call it from the code that I do pass into evaluateString (kind of like the AddCode method works in Microsoft's scripting control). I would like to add code like I can currently add variables by using the ScriptableObject.putProperty method.
Is there a way to do this? Can someone provide a code snippet or a link to the documentation. Thanks!
From the documentation and examples it looks like references to previously evaluated objects are controlled by scopes.
Context context = Context.enter();
try {
ScriptableObject scope = context.initStandardObjects();
Object out = Context.javaToJS(System.out, scope);
ScriptableObject.putProperty(scope, "out", out);
context.evaluateString(scope,
"function foo() { out.println('Hello, World!'); }", "<1>", 1, null);
context
.evaluateString(scope, "function bar() { foo(); }", "<2>", 1, null);
context.evaluateString(scope, "bar();", "<3>", 1, null);
} finally {
Context.exit();
}
(Rhino 1.7 release 2)
I know some people use Rhino directly to get the latest version, but the Java 6 implementation can evaluate scripts like this:
ScriptEngine engine = new ScriptEngineManager().getEngineByExtension("js");
engine.eval("function foo() { println('Hello, World!'); }");
engine.eval("function bar() { foo(); }");
engine.eval("bar();");
In my code I had that need (utility scripts and such), and I just simply concatenated them together in a giant StringBuilder and evaled it (Java 6). Its the only way since javascript can't do (without Java wrapper objects) otherJSScript.someUsefulFunction().
public static native void doConnect() /*-{
$wnd.jQuery(document).trigger('connect',
{
jid: 'sss',
password: 'sss'
}
);
}-*/;
i tried the above ,but there is no error in firebug or gwt hosted mode
console(so i cannot know whether the code is success or not). may i know is this the correct way to call jquery trigger? but when i put alert() in bind('connect'), it was not called
inside .js file
$(document).bind('connect', function (ev, data) {
alert('not call.....at all');
var conn = new Strophe.Connection(
"http://bosh/xmpp-httpbind");
conn.connect(data.jid, data.password, function (status) {
if (status === Strophe.Status.CONNECTED) {
$(document).trigger('connected');
} else if (status === Strophe.Status.DISCONNECTED) {
$(document).trigger('disconnected');
}
});
Hello.connection = conn;
});
I had similar issues when using jQuery UI with GWT - no errors in console/dev mode, yet the code did not behave like I wanted. The reason was that jQuery (and such frameworks) extend/change many core elements of JavaScript and expect it to stay that way - however, GWT code (meaning, also JSNI stuff) is executed from a "clean" iframe (so that no external frameworks can mess with the language and cause some weird errors in GWT, that's why you have to reference to the main window via $wnd).
I'd suggest moving your doConnect function to the host page (or external js file linked to the host page) and instead just call that function from your JSNI stub:
public static native void doConnect() /*-{
$wnd._doConnect('sss','sss'); //_doConnect defined in the host page
}-*/;
Or provide helper functions that will return Arrays, etc, from the host page, so that they include all the changes that jQuery made and expects.
It's a bit late for this answer, but your original code did not work due to a simple mistake: You have properly used $win instead of window but a few characters later you have used document instead of $doc :)
public static native void doConnect() /*-{
$wnd.jQuery($doc).trigger($wnd.jQuery.Event('connect', {
jid: 'sss',
password: 'sss'
}));
}-*/;