Created
November 15, 2013 20:21
-
-
Save jamesfalkner/7490943 to your computer and use it in GitHub Desktop.
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
## AJAXification with well-formed JSON result construction | |
## AJAXification with AUI and JSON parameters | |
#set ($pns = $request.portlet-namespace) | |
#if ($request.lifecycle == "RENDER_PHASE") | |
<body onload="${pns}doit()"> | |
<p><em>Demo 5: AlloyUI AJAX call to WCM App, JSON parameters construcuted with JSONFactory</em></p> | |
<p><a href="${request.resource-url}">See what WCM App's RESOURCE_PHASE emits</a></p> | |
<h4>You should see a randomly updated thing below, with quotes</h4> | |
<div id="${pns}counter"><code>COUNTER</code></div> | |
<img src="http://www.liferay.com/osb-community-theme/images/custom/heading.png"> | |
<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 : "", | |
dataType: "json", | |
on: { | |
success: function () { | |
var responseData = this.get("responseData"); | |
var stat = responseData.stat; | |
if (stat == 'ok') { | |
onSuccess(responseData.result); | |
} else { | |
onError("ERROR: Unable to retrieve results"); | |
} | |
}, | |
failure: function () { | |
onError("Unable to retrieve results"); | |
} | |
} | |
} | |
); | |
} | |
); | |
} | |
function ${pns}doit() { | |
setInterval(function () { | |
${pns}auiXmlHttpRequest('${request.resource-url}', null, function (successObj) { | |
var cnt = document.getElementById('${pns}counter'); | |
cnt.innerHTML = '<code>' + successObj.something + '</code>'; | |
}, function (errmsg) { | |
alert("OH NOES: " + errmsg); | |
}); | |
}, 1000); | |
} | |
</script> | |
</body> | |
#elseif ($request.lifecycle == "RESOURCE_PHASE") | |
#set ($resultWrapper = $jsonFactoryUtil.createJSONObject()) | |
#set ($result = $jsonFactoryUtil.createJSONObject()) | |
#set ($V = $result.put("something", "${escapeTool.q}${randomNamespace}${escapeTool.q}")) | |
#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