Updating function object in different applets via Javascript API

Juan Antonio Macías García shared this question 1 month ago
Answered

Hello everyone.

I have a question regarding communication between two applets via the Javascript API (similar to what is seen in this example).

In one of those applets I'd have a series of sliders and textboxes to control the parameters in the other applet. I am aware that I can update objects such as sliders and points using the setValue and setCoords methods present in the API, but I seem to be unable to do such thing with a function object.

I've tried using the methods above following the example above, also trying out with other methods like setTextvalue. Below is an extract of what I've tried.

function ggbOnInit(param) {
	if (param == "ggbApplet") {
		ggbApplet.registerObjectUpdateListener("f", "funcListener");
	}
}

function funcListener(objName) {
      // get value from applet1 and set value in applet2	
	var changedValue = ggbApplet.getValueString(objName);            
	ggbApplet2.setTextValue(objName, changedValue);		
}
If anyone knows whether is possible or not to do such thing, I'd be grateful.

Thank you.

Best Answer
photo

si pasa la función supongo que pasará como texto y entonces necesitaras usar un comando que incluya una instrucción parsetofunction ¿lo has intentado así?

Comments (5)

photo
2

How do you want the function to change exactly? ie what are the possible functions you want

photo
1

I'd like to change the functions definition, i.e., change it from being a quadratic function (x^2+1) to be a trigonometric one (sin(x)).

photo
photo
2

si pasa la función supongo que pasará como texto y entonces necesitaras usar un comando que incluya una instrucción parsetofunction ¿lo has intentado así?

photo
1

Muchísimas gracias. Poniendo un listener en la función que me diera el texto y después usando ParseToFunction funciona. Dejo abajo como quedaría el código por si alguien lo necesita. Gracias de nuevo.

-------------

Thank you so mucho. Using a listener in the function to get its text and later using ParseToFunction works. Here is the code in case anyone else needs it. Thanks again.


function ggbOnInit(param) {
	if (param == "ggbApplet") {
		ggbApplet.registerObjectUpdateListener("f", "funcListener");
	}
}
function funcListener(objName) {
	var changedValue = ggbApplet.getValueString(objName);            
	ggbApplet2.evalCommand('ParseToFunction(f, "' + changedValue + '")')
}

photo
photo
2

Try

n=1
list1={sin(x),x^2+1}
f(x)=Element(list1,n)


then you can just do

ggbApplet.setValue("n",2);

© 2019 International GeoGebra Institute