Last active
August 29, 2015 14:25
-
-
Save adtennant/f16d9c9f228f2851c073 to your computer and use it in GitHub Desktop.
Code snippets from: http://adtennant.co.uk/blog/2014/03/20/component-controller-communication-using-interfaces-part-1/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class CounterComponentController { | |
public CounterComponentListener listener { get; set; } | |
private Integer count; | |
public CounterComponentController() { | |
count = 0; | |
} | |
public Integer getCount() { | |
return count; | |
} | |
public PageReference increment() { | |
count++; | |
if(listener != null) { | |
listener.onChange(count); | |
} | |
return null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:component controller="CounterComponentController"> | |
<apex:attribute name="componentListener" | |
description="The listener for this component." | |
type="CounterComponentListener" | |
assignTo="{!listener}" | |
/> | |
<apex:outputPanel id="output"> | |
<apex:outputText value="{!count}"/> | |
</apex:outputPanel> | |
<apex:form> | |
<apex:commandButton action="{!increment}" value="Increment"/> | |
</apex:form> | |
</apex:component> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface CounterComponentListener { | |
void onChange(Integer newValue); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<apex:page controller="BasicCounterPageController"> | |
<apex:pageMessage severity="warning" strength="3" summary="It's over 2!" rendered="{!ShowAlert}"/> | |
<c:CounterComponent componentListener="{!ComponentListener}"/> | |
</apex:page> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BasicCounterPageController implements CounterComponentListener { | |
private Boolean showAlert; | |
public BasicCounterPageController() { | |
showAlert = false; | |
} | |
public Boolean getShowAlert() { | |
return showAlert; | |
} | |
public CounterComponentListener getComponentListener() { | |
return this; | |
} | |
public void onChange(Integer newValue) { | |
showAlert = (newValue > 2); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment