I try to convert sample .net application with P/Invoke to javascript with JSIL.
C# code:
[DllImport("JSTestLib", EntryPoint = "Get42", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern int Get42();
Generated javascript:
$.ExternalMethod({Static:true , Public:true }, "Get42",
JSIL.MethodSignature.Return($.Int32)
);
Where should I add implementation of the Get42 method in javascript? Should I register this method manually in JSIL?
I have only an error now:
The external method 'System.Int32 Get42()' of type 'Test.Program' has
not been implemented.
Just use JSIL.ImplementExternals - take a look at JSIL.Core.js for examples
Related
I wanna use the next java script code:
function dumpVal(file) {
if (file !=null) {
var wb = new Workbook.create(file);
var sheet = wb.getSheet("Tabelle1");
for (myrow = 1; !isCellEmpty(sheet, myrow, 0); myrow++) {
dataset.setColumnValue("A",getNumericValue(sheet,myrow,0));
dataset.storeResultRow();
}
}
return;
}
but when I am compiling it I receive the next error message: ReferenceError: "Workbook" is not defined.
Can someone to tell me what am I doing wrong?
You recieve the error with the followig line of code:
var wb = new Workbook;
At the point where you create the workbook (new Workbook) you refer to a class called "Workbook". At this point your script dont have a class called Workbook it.
Solution:
You should check your scripts if the class is included and its naming.
Maybe the class is initialized later!
For debug purposes you can try to create the class a line before:
class Workbook{ }
If you recieve an error now because Workbook needs a method called "create", you know that the class is just missing.
I assume that you want to parse excel file from your web application, the library that you are using is for developing add-ins for excel not web application :
Excel JavaScript API programming overview
This article describes how to use the Excel JavaScript API to build add-ins for Excel 2016. It introduces key concepts that are fundamental to using the APIs, such as RequestContext, JavaScript proxy objects, sync(), Excel.run(), and load(). The code examples at the end of the article show you how to apply the concepts.
source :
https://dev.office.com/docs/add-ins/excel/excel-add-ins-javascript-programming-overview
If you want to parse Excel in your web application I suggest to use this library :
https://github.com/SheetJS/js-xlsx
I did not use it so I cant garanty it, but you can look for similar librarys.
I used to do it by attaching an object
self.page().mainFrame().addToJavaScriptWindowObject("js_interface", self.jsi)
In 5.7 I do:
self.page().setWebChannel(self.jsi)
But I understandibly get a JavaScript error when I try to access exposed functions:
js: Uncaught ReferenceError: js_interface is not defined
Googling around I found that I should use qwebchannel.js, but I couldn't find the file or instructions on how to use it anywhere (there was some info, but only in some examples provided when installing QT, not PyQT).
You can include qwebchannel.js into html page using the script tag:
<script src="qrc:///qtwebchannel/qwebchannel.js"></script>
Then, create a web channel on the python side:
from PyQt5.QtCore import QObject, pyqtSlot
from PyQt5.QtWebChannel import QWebChannel
from PyQt5.QtWebEngineWidgets import QWebEngineView
class CallHandler(QObject):
#pyqtSlot()
def test(self):
print('call received')
view = QWebEngineView()
channel = QWebChannel()
handler = CallHandler()
channel.registerObject('handler', handler)
view.page().setWebChannel(channel)
JS code that interacts with the web channel:
new QWebChannel(qt.webChannelTransport, function (channel) {
window.handler = channel.objects.handler;
window.handler.test();
});
Take a look at this page. It contains a useful example (in c++ but easily translatable into python).
First of all, you have to use a websocket to communicate from html to your app and viceversa.
Then you can set up your QWebChannel.
I think it's big drawback, JS cannot directly communicate with Python in PyQT5.9+ like it used to with "addToJavaScriptWindowObject" command.
And using websockets... what if Firewall is heavy and all ports blocked.
I guess I will rely on simple callback (long pooling type from Python to JS checking for changes/commands) method and no QTWebChannel usage.
I have a custom asp.net server control to display images.What I need now is to draw a rectangle on the center of image and the rectangle should be re sizable by dragging on its edges.Is it possible to accomplish this using JavaScript ?. I need to embed that script in that control. Is it possible ?
You can include a javascript file in a server control.
Add a reference to the assemblyinfo.cs
[assembly: WebResource("Custom.js", "text/javascript")]
Then a reference on the PreRender:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
string resourceName = "Custom.js";
ClientScriptManager cs = this.Page.ClientScript;
cs.RegisterClientScriptResource(typeof(CustomControls.Custom), resourceName);
}
Here is a nice article on the subject
I generated classes in JavaScript from a WSDL by using Visual Studio's WSDL utility.
wsdl /o:SomeClasses.js /l:js https://SomeCompany.com/SomeService?WSDL
The output contains classes (in JavaScript) that look like this:
public System.CodeDom.Compiler.GeneratedCodeAttribute("wsdl", "4.0.30319.1") System.SerializableAttribute() System.Diagnostics.DebuggerStepThroughAttribute() System.ComponentModel.DesignerCategoryAttribute("code") System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:some.company")
class SomeUser {
private var domainNameField : System.String;
private var userNameField : System.String;
///<remarks/>
public final function get domainName() : System.String {
return this.domainNameField;
}
public final function set domainName(value : System.String) {
this.domainNameField = value;
}
///<remarks/>
public final function get userName() : System.String {
return this.userNameField;
}
public final function set userName(value : System.String) {
this.userNameField = value;
}
}
Is it possible to write OOP JavaScript utilizing these classes? If so, what is the syntax, examples, etc.
When you specify the JS language for the Web Services Description Language Tool, you are NOT specifying JavaScript, but JScript. It's not even JScript that InternetExplorer can fully understand, it's JScript.NET.
JScript.NET is a server side scripting language based on JScript but with added features - available only on the server side - like the class you've got in the code you posted.
You should look for other ways of generating JavaScript code, maybe with a tool like Wsdl2js or performing your WS call with JQuery. You can't use the Wsdl.exe generated code inside InternetExplorer as InternetExplorer only supports the old JScript language (and non IE browsers don't even support that).
I have a wicket application in which I have added the javascript files within the markup html:
<script src="script/jquery.min.js" type="text/javascript"></script>
My javascript files are not placed beside my .java or .html files, they are in different location in the server as can be seen on previous script declaration.
My question is: Is it possible to add these javascript files depending on the application mode? I.E. if the application is in development mode, load one javascript file, if it is in production load this other one.
Thanks!
PS: the idea is to load "min" version on production but the extended files on development so debugging becomes posible
NOTE: Watching different answers here I re-state: the problem is not finding when the wicket app is in development or deployment mode, I know that, but is about how to change html markup or adding different JavaScript resources
extendig the answer of #rotsch you can do it in wicket 1.5 with :
#Override
public void renderHead(IHeaderResponse response) {
if(DEVELOPMENT)
response.renderString("<script type=\"text/javascript\" src=\"url1\"></script>");
else
response.renderString("<script type=\"text/javascript\" src=\"url2\"></script>");
}
https://cwiki.apache.org/WICKET/migration-to-wicket-15.html#MigrationtoWicket1.5-RemovedHeaderContributorandfriends.
You can find out in which mode you are with the following code:
RuntimeConfigurationType.DEPLOYMENT.equals(getApplication().getConfigurationType())
or
RuntimeConfigurationType.DEVELOPMENT.equals(getApplication().getConfigurationType())
I use this directory layout:
resources
|---JQueryResource.java
|---jquery-1.6.4.js
|---jquery-1.6.4.min.js
With this class:
public class JQueryResource {
/**
* Must be called in a RequestCycle.
*
* #return Url for the jQuery library.
*/
public static String getURL() {
if (Application.get().usesDevelopmentConfig()) {
Url url =
RequestCycle.get().mapUrlFor(
new PackageResourceReference(JQueryResource.class, "jquery-1.6.4.js"),
null);
return url.toString();
} else {
Url url =
RequestCycle.get().mapUrlFor(
new PackageResourceReference(JQueryResource.class,
"jquery-1.6.4.min.js"), null);
return url.toString();
}
}
}
This is how I add the resource to my page.
#Override
public void renderHead(IHeaderResponse a_response) {
a_response.renderJavaScriptReference(JQueryResource.getURL());
}
You could use pack:tag to compress all your resources: http://sourceforge.net/projects/packtag/
In your web.xml/.properties file you can specify whether to pack it or not depending on your production mode.
I set a property in a properties file with I add to the path when starting the VM.
Then I do a if else similar to the PHP answer.