I sort of got it to work eventually...
I replaced the use of
<aui:script> with just ordinary HTML's
<script> tags.
The portlet however, even after reload in Liferay, doesn't use the new code immediately; took me a few refresh of the page before the new JS kicked in. I assume a clear cache might do the trick; I suspect my browser, Safari 5.1, may be doing some caching + optimisation of JS scripts.
I also found this to be useful in helping me understand how AUI's Node is related to HTML's DOM nodes:
Liferay's Blog - Alloy UI - Working with elements and eventsThe above article also serves as intro. to using Alloy UI.
Here is a sample JSP (
view.jsp) that should work:
<script type="text/javascript">
var portletNamepsace = '<portlet:namespace />';
// alert('From view.jsp\n' + portletNamepsace);
</script>
<aui:form id="my-aui-form">
<aui:input type="textarea" label="My Data"
rows="5" cols="30"
id="my-aui-textarea" name="aui-text" />
<span id="<portlet:namespace />aui-char-count"></span> character(s) remaining.
</aui:form>
This in conjunction with project/docroot/js/main.js:
AUI().ready('node', 'aui-char-counter', function(A) {
// alert('From main.js\n' + portletNamepsace);
var maxTextLength = 148;
var auiTextAreaIDWithNSpace = '#' + portletNamepsace + 'my-aui-textarea';
var auiCountAreaIDWithNSpace = '#' + portletNamepsace + 'aui-char-count';
var auiTextAreaNode = new A.one(auiTextAreaIDWithNSpace);
if (auiTextAreaNode === null) {
alert('From main.js\n' + 'AUI Text Area node not found.');
}
var auiCountAreaNode = new A.one(auiCountAreaIDWithNSpace);
if (auiCountAreaNode === null) {
alert('From main.js\n' + 'AUI Count Text node not found.');
}
auiCountAreaNode.set('text',
(maxTextLength - (auiTextAreaNode.get('text').length)));
var auiTextCounter = new A.CharCounter( {
input: auiTextAreaIDWithNSpace,
counter: auiCountAreaIDWithNSpace,
maxLength: maxTextLength
});
});
In
liferay-portlet.xml, I have
main.js as:
<footer-portlet-javascript>/js/main.js</footer-portlet-javascript>
I think the above code works because the
<script> tag is parsed before
main.js, since
main.js is in footer. Assuming entire JavaScript is executing in a single space, what I have in
<script> tag is also visible in
main.js.
Please sign in to flag this as inappropriate.