Skip to content

Instantly share code, notes, and snippets.

@eduardolundgren
Created November 20, 2012 05:51
Show Gist options
  • Save eduardolundgren/4116282 to your computer and use it in GitHub Desktop.
Save eduardolundgren/4116282 to your computer and use it in GitHub Desktop.
liferay.js
Liferay = window.Liferay || {};
(function(A, Liferay) {
var Lang = A.Lang;
var owns = A.Object.owns;
Liferay.namespace = A.namespace;
A.mix(
AUI.defaults.io,
{
method: 'POST',
uriFormatter: function(value) {
return Liferay.Util.getURLWithSessionId(value);
}
},
true
);
var ServiceUtil = {
invoke: function(payload, ioConfig) {
var instance = this;
if (ioConfig.method && (ioConfig.method.toUpperCase() == 'GET')) {
ioConfig.cache = false;
}
if (Liferay.PropsValues.NTLM_AUTH_ENABLED && Liferay.Browser.isIe()) {
ioConfig.method = 'GET';
}
return A.io.request(
themeDisplay.getPathContext() + '/api/jsonws/invoke',
A.merge(
{
data: {
cmd: A.JSON.stringify(payload),
p_auth: Liferay.authToken
},
dataType: 'json'
},
ioConfig
)
);
},
parseInvokeArgs: function(args) {
var instance = this;
var result;
var ioConfig;
var payload = args[0];
if (owns(payload, 'io')) {
ioConfig = payload.io;
payload.io = undefined;
}
if (Lang.isString(args[0])) {
result = instance.parseInvokeStringArgs(args, payload, ioConfig);
}
else {
result = [
payload,
ioConfig
];
}
return result;
},
parseInvokeStringArgs: function(args, payload, ioConfig) {
var instance = this;
var methodParams = {};
var callbackIndex = 1;
payload = {};
if (Lang.isObject(args[1]) && !Lang.isFunction(args[1])) {
methodParams = args[1];
callbackIndex++;
}
payload[args[0]] = methodParams;
if (Lang.isFunction(args[callbackIndex])) {
var successCallback = args[callbackIndex];
var failureCallback = args[callbackIndex + 1];
ioConfig = {
on: {
failure: function(event) {
var data = this.get('responseData');
failureCallback.call(null, data);
},
success: function(event) {
var data = this.get('responseData');
successCallback.call(null, data);
}
}
};
}
return [
payload,
ioConfig
];
}
};
/**
* OPTIONS
*
* Required
* service {string|object}: Either the service name, or an IO configuration object containing a property named service.
*
* Optional
* data {object|node|string}: The data to send to the service. If the object passed is the ID of a form or a form element, the form fields will be serialized and used as the data.
* successCallback {function}: A function to execute when the server returns a response. It receives a JSON object as it's first parameter.
* exceptionCallback {function}: A function to execute when the response from the server contains a service exception. It receives a the exception message as it's first parameter.
*/
var Service = function() {
var instance = this;
var args = ServiceUtil.parseInvokeArgs(arguments);
ServiceUtil.invoke.apply(ServiceUtil, args);
};
Liferay.Service = Service;
var components = {};
var componentsFn = {};
Liferay.component = function(id, value) {
var retVal;
if (arguments.length === 1) {
var component = components[id];
if (component && Lang.isFunction(component)) {
componentsFn[id] = component;
component = component();
components[id] = component;
}
retVal = component;
}
else {
retVal = (components[id] = value);
}
return retVal;
};
Liferay._components = components;
Liferay._componentsFn = components;
Liferay.Template = {
PORTLET: '<div class="portlet"><div class="portlet-topper"><div class="portlet-title"></div></div><div class="portlet-content"></div><div class="forbidden-action"></div></div>'
};
})(AUI(), Liferay);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment