Skip to content

Instantly share code, notes, and snippets.

@jamesfalkner
Created November 15, 2013 20:21
Show Gist options
  • Select an option

  • Save jamesfalkner/7490947 to your computer and use it in GitHub Desktop.

Select an option

Save jamesfalkner/7490947 to your computer and use it in GitHub Desktop.
## AJAXification with well-formed JSON result construction and parameter passing
#set ($pns = $request.portlet-namespace)
#if ($request.lifecycle == "RENDER_PHASE")
<body onload="${pns}doit()">
<img
src="http://www.liferay.com/osb-community-theme/images/custom/heading.png"
style="float:right;width:20%">
<p><em>Demo 6: AlloyUI AJAX call, pass parameters and get JSON results</em></p>
<p>
<h3>You should get two separate alerts!</h3></p>
<em>Raw output from RESOURCE_PHASE:</em>
<div id="${pns}resourcePhaseOutput"></div>
<script type="text/javascript">
function ${pns}auiXmlHttpRequest(url, data, onSuccess, onError) {
AUI().use(
"aui-base", "aui-io-plugin", "aui-io-request",
function (A) {
A.io.request(
url,
{
data: data ? data : "dummy",
dataType: "json",
on: {
success: function (event, id, obj) {
var responseData = this.get("responseData");
var stat = responseData.stat;
if (stat == 'ok') {
onSuccess(responseData.result);
} else {
onError("error: " + stat);
}
},
failure: function (event, id, obj) {
onError("error: " + JSON.stringify(event));
}
}
}
);
}
);
}
function ${pns}doit() {
${pns}auiXmlHttpRequest('${request.resource-url}', {
"${pns}title": "North America Symposium",
"${pns}location": "San Francisco"
}, function (successObj) {
var raw = document.getElementById('${pns}resourcePhaseOutput');
raw.innerHTML = '<pre>' + JSON.stringify(successObj) + '</pre>';
alert("got something: " + successObj.TITLE);
alert("got something: " + successObj.LOCATION);
}, function (errmsg) {
});
}
</script>
</body>
#elseif ($request.lifecycle == "RESOURCE_PHASE")
#set ($resultWrapper = $jsonFactoryUtil.createJSONObject())
#set ($result = $jsonFactoryUtil.createJSONObject())
#set ($V = $result.put("TITLE", ${request.parameters.title}))
#set ($V = $result.put("LOCATION", ${request.parameters.location}))
#set ($V = $resultWrapper.put("stat", "ok"))
#set ($V = $resultWrapper.put("result", $result))
$resultWrapper
#end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment