function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kkaalkkaal 

Set and read variables in controller from javascript

I want to catch mouse events (onmouseenter, onmouseover etc) and set variables in my controller object. For this, I would need to call the setter and getter.

Could you plse point me to a document(s) which explain how to do that?

 

Thanks

TehNrdTehNrd

You could do this but I am going to challenge and ask why? Those events occur pretty quick and this would requite a post back to the server on every event which is slow. You could end up having a user fire all of these events and all of the calls to the controller may not execute in the correct order or at all.

kkaalkkaal

What I really want to do is to move my mouse of an object and that changes the content of a block on the page. This block should retrieve new data from my extended Controller. For that call, I also need parameter that the controller knows what data to return.

TehNrdTehNrd

Gotcha, you can do something like this. I just threw this together in notepad so there may be some syntax issues.

 

 

<apex:page controller="MyController">
	<apex:outputPanel>
		View Something 1
		<apex:actionSupport value="onmouseover" action="{!updateContent}" rerender="content">
			<apex:param name="viewId" value="someId" assignTo="{!viewId}"/>
		</apex:actionSupport>
	</apex:outputPanel>
	
	<apex:outputPanel>
		View Something 2
		<apex:actionSupport value="onmouseover" action="{!updateContent}" rerender="content">
			<apex:param name="viewId" value="someId" assignTo="{!viewId}"/>
		</apex:actionSupport>
	</apex:outputPanel>
	
	<apex:outputPanel id="content">
		{!myObject.Name}
	</apex:outputPanel>
</apex:page>

public class MyController{

	public String viewId {get; set;}
	public Opportunity myObject {get; set;}
		
	public void updateContent(){
		myObject = [select Name from Opportunity where Id = :viewId];
	}
}

 

 

kkaalkkaal

Thank you for this valueable hint.

But I really need to set call functions WITH parameters in the controller object from Javascript. Any solution for that? (asynchronous call or so?)

TehNrdTehNrd

You can't call a method with parameters but this:

 

public String viewId {get; set;}
public Opportunity myObject {get; set;}

public void updateContent(){
myObject = [select Name from Opportunity where Id = :viewId];
}

Does the same thing as this.

public Opportunity myObject {get; set;}

public void updateContent(String viewId){
myObject = [select Name from Opportunity where Id = :viewId];
}