(This is the plugin I am trying to use) http://zurb.com/playground/twentytwenty
I have
app/design/frontend/myvendor/mytheme/requirejs-config.js
and it looks like:
var config = {
map: {
'eventmove': 'Magento_Theme/js/jquery.event.move',
'twentytwenty': 'Magento_Theme/js/jquery.twentytwenty'
},
shim: {
'twentytwenty': {
deps: ['jquery']
}
}
};
cleared pub folder, set development mode, re-compile, deploy static files...
viewing source on front end reveals the js files are not loaded.
also, this is the code on the detail page that it needs, I dont knwo if it's formatted right or not since requirejs isnt working.
<script>
require([
'jquery',
'eventmove',
'twentytwenty'
], function () {
jQuery(document).ready(function() {
jQuery(window).load(function() {
jQuery(".twentytwenty-container").twentytwenty();
});
});
</script>
-
Is there any browser console error?Khoa Truong– Khoa Truong2016年12月13日 00:35:48 +00:00Commented Dec 13, 2016 at 0:35
2 Answers 2
Magento2 uses RequireJS to load javascript modules, you should use this to integrate the module with the standard jQuery library. You should place a requirejs-config.js file in your custom theme. This file is used by Magento for locating JS modules, like for example twentytwenty.
Example requirejs-config.js:
var config = {
paths: {
'twentytwenty': 'Magento_Theme/js/twentytwenty'
},
shim: {
'twentytwenty': {
deps: ['jquery']
}
}
};
paths is used by Magento2 to locate the main JS module. Shim makes sure that it is loaded after jQuery
File location: app/design/frontend/Your/CustomTheme/Magento_Theme/web/js/twentytwenty.js
In your template PHTML:
<script>
require([
'jquery',
'twentytwenty'
], function () {
jQuery(window).load(function() {
jQuery("#container1").twentytwenty();
});
});
</script>
Do not add JS files directly to the <head> tag. You can find more documentation about RequireJS here: http://alanstorm.com/magento_2_and_requirejs/
-
Thanks so Much @N-Bursma I edited the original question to reflect the new changes, I tried that, and it requirejs isn't loading the .js at allM21– M212016年12月13日 18:34:19 +00:00Commented Dec 13, 2016 at 18:34
-
What kind of error does the console give you? I'm not sure that the location
'js/jquery.event.move'is correctly. Try to stick withMagento_Theme/js/twentytwentyfor now, I can confirm that this locations is working.Tjitse– Tjitse2016年12月13日 18:59:25 +00:00Commented Dec 13, 2016 at 18:59 -
I've also updated the PHTML snippet, you should try that code. Make sure that you delete the requirejs file in the /pub folder. Also try to open the page with cache disabled or in incognito mode.Tjitse– Tjitse2016年12月13日 19:02:34 +00:00Commented Dec 13, 2016 at 19:02
-
thanks so much, so changing it to Magento_Theme/js/**** it still doesn't work... the console shows a: Unable to resolve the source file for 'frontend/CVP/CVPStyle1-2/en_US/twentytwenty.js'M21– M212016年12月13日 21:16:41 +00:00Commented Dec 13, 2016 at 21:16
-
I've added the exact file location to the answer. Can you compare this to the location of your twentytwenty.js file? Make sure that you aren't missing the
webfolder inMagento_Theme, which is standard for js/css/images.Tjitse– Tjitse2016年12月14日 07:12:07 +00:00Commented Dec 14, 2016 at 7:12
@Tjitse:
Have you tried to wrap the 3rd party JS library (code in the jquery.twentytwenty.js) in "define", as follows:
define(["jquery"], function($){
(function($){
$.fn.twentytwenty = function(options) ...
// ... the rest of library code ...
// ...
})(jQuery);
});
Doing this should make sure your code runs after the dependencies are loaded.
BTW, your requirejs-config.js looks just fine, but I guess the jquery.event.move.js also requires jquery?
-
I'm sure jquery.event.move.js does also require jquery. so are you saying to modify the jquery.twentytwenty.js file to have the 'define' as you posted?M21– M212017年01月22日 16:53:28 +00:00Commented Jan 22, 2017 at 16:53
-
Yes, basically. Define ensures requirements are loaded beforeDmitri Sologoubenko– Dmitri Sologoubenko2017年01月23日 15:38:31 +00:00Commented Jan 23, 2017 at 15:38
-
You can also use AMD features (many recent jquery libs and jquery itself do that)Dmitri Sologoubenko– Dmitri Sologoubenko2017年01月23日 15:39:24 +00:00Commented Jan 23, 2017 at 15:39
Explore related questions
See similar questions with these tags.