Closed caneta closed 8 years ago
You could easily AMDify the provided global jQuery by placing a simple snippet in your project.
To do so create a frontend-js-jquery-amd-provider bundle with the following structure:
frontend-js-jquery-amd-provider/
├── bnd.bnd
├── build.gradle
└── src/
└── main/
└── resources/
└── META-INF/
└── resources/
└── config.js
Where config.js is:
define('jquery', function () {
return window.jQuery;
});
and bnd.bnd is:
Bundle-Name: jQuery AMD Contributor
Bundle-SymbolicName: my.pkg.frontend.js.jquery.amd.contributor
Bundle-Version: 1.0.0
Liferay-JS-Config: /META-INF/resources/config.js
Thank you @yuchi, your solution worked! I'll share my steps:
Inside my liferay workspace I've created a new bundle
blade create -t mvcportlet my-pkg-frontend-js-jquery-amd-provider
liferayworkspace/modules/my-pkg-frontend-js-jquery-amd-provider
tree as you describedI've modified bnd.bnd a little:
Bundle-Name: My jQuery AMD Provider
Bundle-SymbolicName: my.pkg.frontend.js.jquery.amd.provider
Bundle-Version: 1.0.0
Web-ContextPath: /my-pkg-frontend-js-jquery-amd-provider
Liferay-JS-Config: /META-INF/resources/config.js
I've deployed it
./gradlew :modules:my-pkg-frontend-js-jquery-amd-provider:deploy
Inside my themes/mytheme/js/main.js I've put the following:
'use strict';
Loader.addModule({
name : 'slick',
dependencies: ['jquery'],
path : '/o/my-theme/components/slick-carousel/slick/slick.min'
});
require(['slick'],
function(slick) {
console.log(jQuery.fn.slick); // It prints the function source
console.info('It works!');
},
function(error) {
console.error(error);
}
);
});
You can also add a config.js in your theme. Leave the AMDification of jQuery in its own bundle, but move the snippet with Loader.addModule
there, with this you'll have a cleaner main.js.
To do so you can leverage the fact that a theme WAR is actually deployed as a bundle, at the of the day. If you add a src/META-INF/MANIFEST.mf file with just the following content:
Liferay-JS-Config: /js/config.js
then it will be treated as other bundles and the config file used at every page request.
;)
I've moved the Loader.addModule
function inside my src/config/config.js file and put the line Liferay-JS-Config: /config/config.js
inside src/META-INF/MANIFEST.mf, but it didn't work: it searches for a http://localhost:8080slick.js (no slashes after port number), which is incorrect.
But putting Liferay-JS-Config=/config/config.js
inside my src/WEB-INF/liferay-plugin-package.properties it worked as expected!
Great tip!! Didn't know liferay-plugin-package.properties is merged in the manifest!
Hehehe...say thank you to @jbalsas! He gave me this hint on Liferay Forums.
Hi guys, I'm facing problems with jQuery plugins again, like in #80. I'm trying to get working this carousel plugin, which source code starts with the following function:
In a Liferay environment, it falls into the first if statement, but the module 'jquery' is not present into the portal and so I get the error that the following script is missing:
And this is coherent with the Liferay AMD Loader I guess, because the jquery definition in the plugin does not match with the path of jQuery library inside the portal (the everything.jsp file). I solved temporarily changing the first if statement like this:
that is: if I am in a Liferay environment, don't consider the 'jquery' module but go on and fall in the third case (
factory(jQuery);
)Since we've got tons of jQuery based plugins out there, I'm searching for a better approach. Is there a way to have loaded jQuery as a module named 'jquery' with the correct Liferay path, satisfying plugins like this one?
Thank you