JScript to Javascript Conversion - javascript

The code below is a web service call to ICEPortal which is in JScript.NET format. Currently Iceportal doesn't have a webservice call using javascript. Has anybody done this using javascript? I need your help to convert the code below to javascript format.
// JScript.NET
var h:ICEPortal.ICEWebService = new ICEPortal.ICEWebService();
var myHeader:ICEPortal.ICEAuthHeader = new ICEPortal.ICEAuthHeader();
myHeader.Username = "distrib#distrib.com";
myHeader.Password = "password";
h.ICEAuthHeaderValue = myHeader;
var brochure:ICEPortal.Brochure;
var ErrorMsg;
var result = h.GetBrochure("MyMappedID", ErrorMsg, brochure);

I think you just need to remove the type definitions (in bold below):
var myHeader :ICEPortal.ICEAuthHeader = new ICEPortal.ICEAuthHeader();)
No idea what the ICEPortal classes are, but if they are available to your Javascript in the global namespace, the following should work. I've added these stubs in for the ICEPortal to test and it works fine for me in Chrome.
You'll obviously want to remove the stubs.
// stubbing out ICEPortal(s)
ICEPortal = {};
ICEPortal.ICEWebService = function() { return true; };
ICEPortal.ICEAuthHeader = function() { return true; };
ICEPortal.ICEWebService.prototype.GetBrochure = function() { return true; };
// end stubbing ICEPortal(s)
var h = new ICEPortal.ICEWebService();
var myHeader = new ICEPortal.ICEAuthHeader();
myHeader.Username = "distrib#distrib.com";
myHeader.Password = "password";
h.ICEAuthHeaderValue = myHeader;
var brochure;
var ErrorMsg;
var result = h.GetBrochure("MyMappedID", ErrorMsg, brochure);

Related

loadContextGoodies is not defined

This is my pretty straight forward firefox plugin main.js. I run it and get 'loadContextGoodies is not defined', what is going wrong here?
const {Cc, Ci, Cu, Cr} = require("chrome");
var events = require("sdk/system/events");
var utils = require("sdk/window/utils");
var { MatchPattern } = require("sdk/util/match-pattern");
var pattern = new MatchPattern(/^https?:\/\/example\.com.*/);
function listener(event) {
var channel = event.subject.QueryInterface(Ci.nsIHttpChannel);
var url = event.subject.URI.spec;
if (isToBeRedirected(url)) {
channel.cancel(Cr.NS_BINDING_ABORTED);
var goodies = loadContextGoodies(channel);
var domWin = goodies.aDOMWindow;
var gBrowser = goodies.gBrowser;
var browser = goodies.browser;
var htmlWindow = goodies.contentWindow;
browser.loadURI("about:blank");
}
}
exports.main = function() {
events.on("http-on-modify-request", listener);
}
function isToBeRedirected(url) {
return pattern.test(url)
}
edit: I was totally overlooking the part of the source I used for the redirecting bit which contained the declaration of the function. I didnt notice it was a scrollbox.. Thanks for the answer though.
According to that answer : https://stackoverflow.com/a/22429478/1170900
You need to declare loadContextGoodies

Is it possible send my object with methods (function) from server to client?

I have object whith method (function) on server (node.js).
I want get this object with methods in browser.
Is it possible?
I made simple example on GitHub
There are server.js and index.html files. Run --> node server.
++++++++ UPD (after comments)
My object on server:
function ClassOrder(name, cost) {
this.name = name;
this.cost = cost;
this.parts = [];
this.summ = function() {
var summ = 0;
this.parts.forEach(function(part) {
summ += part.summ();
});
return summ + this.cost;
};
}
var myobject = new ClassOrder('my object', 10);
myobject.parts[0] = new ClassOrder('my first part', 20);
myobject.parts[1] = new ClassOrder('my second part', 30);
myobject.parts[1].parts[0] = new ClassOrder('my first in second part', 40);
console.log(myobject); // view myobject in console
console.log(myobject.summ()); // return 100 (10+20+30+40)
send to client (on request)
res.end(JSON.stringify(myobject));
and get it on client's javascript code (in browser)
function getobject() {
var xhr = new XMLHttpRequest();
xhr.open('get', '/getobject', true);
xhr.onreadystatechange = function() {
if (xhr.readyState != 4) return;
var myobject = JSON.parse(this.responseText);
console.log(myobject); // all data ok
console.log(myobject.summ()); // no methods!!
};
xhr.send(null);
}
It is a simple example and in real i use prototype (or inherit in util(node.js)).
I use this methods on server and save it in DB. All work is ok.
But if i wont use it on client i need copy-paste all methods, WHY?
Also i dont know how add my example method to my object in client without disassemble it.
The question was: how can I get on server and client identical objects with methods without doublecoding.
There are two way do it.
The first way is convert my methods (function) to text and save it with object. Then we can use Function constructor to run it.
function ClassOrder(name, cost) {
this.name = name;
this.cost = cost;
this.parts = [];
this.summfunc = '\
var summ=0; \
obj.parts.forEach(function(part) { \
summ += part.summ(); \
}); \
return summ + obj.cost;';
}
ClassOrder.prototype.summ = function() {
return (new Function('obj', this.summfunc))(this);
};
and can use it in browser
var myobject = JSON.parse(this.responseText);
myobject.constructor.prototype.summ = function() {
return (new Function('obj', this.summfunc))(this);
};
Link to commit on new branch on GItHub
This way is bad because i need all my methods convert to text. And how say #Felix Kling it makes the code harder to maintain and follow.
The second way is use one file with classes and methods on server and on client. And ufter receive data "convert" it to my class.
my class in file order.js (part of file):
function Order(object) {
object.parts = object.parts || [];
var partsArr = [];
object.parts.forEach(function (value, key) {
partsArr[key] = new Order(value);
});
this.name = object.name || 'Default';
this.cost = object.cost || 0;
this.parts = partsArr;
}
Order.prototype.summ = function() {
var summ = 0;
this.parts.forEach(function(part) {
summ += part.summ();
});
return summ + this.cost;
};
require order.js and change arguments of call new Object:
var Order = require('./order')
var myobject = new Order({name:'my object', cost: 10});
myobject.parts[0] = new Order({name:'my first part', cost: 20});
myobject.parts[1] = new Order({name:'my second part', cost: 30});
myobject.parts[1].parts[0] = new Order({name:'my first in second part', cost: 40});
and finaly use this file on client:
<script src='order.js'></script>
...
var xhrobj = JSON.parse(this.responseText);
var myobject = new Order(xhrobj);
Link to commit on other branch on GItHub
I think this way is better then first.
I think the answer is right there: Calling a JavaScript function returned from an Ajax response
But it seems that isn't necessary to put the answers in a script tag, try this:
eval(this.responseText)

JSON get data (swf) to HTML5

So my swf file request an url request (pr2hub.com/get_player_info_2.php?name=NAME)
And my swf use this datas to change movieclip's frames, colors.
I converted it to HTML5, but It's not work.
Original SWF is work:
http://tulyita.hu/games/pr2setspreview.html?users=sothal
HTML5 not work:
http://tulyita.hu/games/pr2setspreview.swf.html?users=sothal
The errors:
The ActionScript class JSON is not supported.
The ActionScript method JSON.parse() is not supported.
Can someone help this with me? AS3 Flash Code:
var loadedDataType:String;
var allowedToLoad:Boolean = false;
var pr2loaderInfo:String;
var pr2data:Object;
function loadPR2data():void
{
var urlRequest:URLRequest = new URLRequest("http://pr2hub.com/get_player_info_2.php?name=" + userName);
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, completeHandler);
urlLoader.load(urlRequest);
}
function completeHandler(event:Event):void
{
var loader:URLLoader = URLLoader(event.target);
pr2loaderInfo = loader.data;
pr2data = JSON.parse(loader.data);
loadRequest_function();
}
function loadRequest_function():void
{
loadHatFrame = pr2data.hat;
loadHatColor1 = pr2data.hatColor;
loadHatColor2 = pr2data.hatColor2;
loadHeadFrame = pr2data.head;
loadHeadColor1 = pr2data.headColor;
loadHeadColor2 = pr2data.headColor2;
loadBodyFrame = pr2data.body;
loadBodyColor1 = pr2data.bodyColor;
loadBodyColor2 = pr2data.bodyColor2;
loadFeetFrame = pr2data.feet;
loadFeetColor1 = pr2data.feetColor;
loadFeetColor2 = pr2data.feetColor2;
}
You need to import JSON first (add corelib.swc if not yet part of the project):
import com.adobe.serialization.json.JSON;
and then :
pr2data = JSON.decode( value );
You can find corelib here : https://github.com/mikechambers/as3corelib#readme

How to pass parameters to local Prettydiff.com

I've downloaded a copy of PrettyDiff to embed in my own local application, so I can compare two AJAX loaded files that are in two variables.
Unfortunately, I can't seem to make prettydiff work. Here's how I try :
var example1 = getFile('exemple1.txt');
var example2 = getFile('exemple2.txt');
var output = prettydiff("/*prettydiff.com api.lang: 8, api.mode: diff, api.diffview: inline, api.source:example1, api.diff: example2 */");
document.getElementById("output").innerHTML = output[0];
All I get is "Error: Source sample is missing.".
I've also tried to make an "api" variable that I fill with the parameters, but that doesn't work either.
The documentation doesn't give any example on how to pass the parameters (options, source and diff texts).
Anyone knows ?
Ok, I found out a way to get it working. I still don't know the fuss about the parameters as comments as specified in the docs, but you can create a js object and pass all your parameters :
var api = new Object();
api.mode = "diff";
api.diffview = "inline";
api.lang = 8;
api.source = example1;
api.diff = example2;
var output = prettydiff(api);
You can use prettydiff option like this.
const prettydiff = require("prettydiff");
let options = prettydiff.options;
options.source = content_Old;
options.diff = content_New;
options.diff_format = "html";
options.lang = "script";
options.color = "white";
options.diff_space_ignore = false;
options.diff_view = "sidebyside";
options.lexer = "script";
options.sourcelabel = "Original File";
options.difflabel = "Updated File";
options.mode = "diff";
options.parse_format = "htmltable";
options.list_options = true;
options.crlf = false;
options.force_indent = true;
outputHtml = prettydiff();

Use JScript to insert Price List on Quote onOpen in Microsoft Dynamics CRM 2011

Here is my code, I get an object error onLoad. Please help.
function Setlook()
{
var lookup = new Object();
var lookupValue = new Array();
lookup.id = "7b31D4D998-F124-E111-96C3-1CC1DEEA";
lookup.entityType = 1022;
lookup.name = "Default";
lookupValue[0] = lookup;
Xrm.Page.getAttribute(“pricelevelid”).setValue(lookupValue);
}
The code itself looks correct, but the GUID of the lookup doesn't. It doesn't have the right format nor does it have the right number of characters (32). Fixing that should eliminate the error.
Here is the proper syntax, the important thing is to have the correct .typename
function Setlook()
{
var value = new Array();
value[0] = new Object();
value[0].id = '{31D4D998-F124-E111-96C3-1CC1DEE8EC2D}';
value[0].name = 'Default';
value[0].typename = 'pricelevel';
Xrm.Page.getAttribute("pricelevelid").setValue(value);
}

Categories

Resources