问题
I'm trying to add jQuery UI's datepicker to my Liferay 7 portlet, yet I keep getting this error:
Object doesn't support property or method 'datepicker'
I'm setting up the dependency like this:
@Component(
immediate = true,
property = {
"com.liferay.portlet.display-category=category.tests",
"com.liferay.portlet.header-portlet-javascript=https://code.jquery.com/ui/1.12.1/jquery-ui.js",
"com.liferay.portlet.instanceable=true",
"javax.portlet.display-name=Advanced Date Picker",
"javax.portlet.init-param.template-path=/",
"javax.portlet.init-param.view-template=/view.jsp",
"javax.portlet.resource-bundle=content.Language",
"javax.portlet.security-role-ref=power-user,user"
},
service = Portlet.class
)
So all I can see on my portlet is the input field without any scripted functionality. I read that since version 7, Liferay has preimplemented a basic jQuery library, so I do not need to download locally and refer to it.
Is there any way to use this the mentioned datepicker, or should I use AlloyUI's one?
回答1:
If you want to integrate liferay 7 theme/portlet/application with jQuery UI, you need to make subtle change in jQuery UI library itself.
By default library starts with -
(function( factory ) { if ( typeof define === "function" && define.amd ) { // AMD. Register as an anonymous module. define([ "jquery" ], factory ); } else { // Browser globals factory( jQuery ); } }(function( $ ) {
If you add library directly in your liferay application, you will be getting an error - Mismatched anonymous define() module:
To overcome this issue, you need to change the library like
(function( factory ) { factory( jQuery ); }(function( $ ) {
What you are doing is removing the reference of define call, which causes the issue, as jQuery is already loaded by default. Same thing you need to perform for other JS libraries which contains the call.
来源:https://stackoverflow.com/questions/42675161/how-to-add-jquery-uis-datepicker-to-a-liferay-7-portlet