Monkey to javascript

Monkey Targets Forums/HTML5/Monkey to javascript

Jesse(Posted 2015) [#1]
javascript noob!
does anybody know how I can pass code from Monkey so that JS would understand it as something like this:
var style = { font: "65px Arial", fill: "#ff0044", align: "center" };


I created a class with those fields but for some reason I don't think it's reading it correctly.


DruggedBunny(Posted 2015) [#2]
You need to wrap your Javascript code into a JS function, within a separate .js file:

// test.js

function testing(message)
{
	alert(message);
}



Import your .js file and declare a Monkey function within an Extern section, with the same parameters/return type as the Javascript function -- don't forget Public after the Extern section:

Import mojo
Import "test.js"

Extern

	Function Alert (msg:String) = "testing"

Public

Class Game Extends App

	Method OnCreate ()
		SetUpdateRate 60
		Alert "Hello!"
	End
	
End

Function Main ()
	New Game
End


Not sure how your code would work (not that hot on CSS/JS!), but presumably you could wrap that in a JS function that also applies the style.

You might want to do some #If TARGET=html5 wraparound stuff if going cross-platform.


Jesse(Posted 2015) [#3]
Excuse me if I am wrong but Isn't your example just passing a string to js? My example creates an inline Class which is not possible in MX. I was looking for an alternative to handle this. I might just create a js class with all of the relevant fields and just wrap it to MX. I think that's my best option.