0

(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>

EDIT description of errors

asked Dec 12, 2016 at 16:42
1
  • Is there any browser console error? Commented Dec 13, 2016 at 0:35

2 Answers 2

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/

answered Dec 12, 2016 at 22:18
9
  • 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 all Commented 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 with Magento_Theme/js/twentytwenty for now, I can confirm that this locations is working. Commented 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. Commented 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' Commented 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 web folder in Magento_Theme, which is standard for js/css/images. Commented Dec 14, 2016 at 7:12
-1

@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?

answered Jan 13, 2017 at 14:57
3
  • 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? Commented Jan 22, 2017 at 16:53
  • Yes, basically. Define ensures requirements are loaded before Commented Jan 23, 2017 at 15:38
  • You can also use AMD features (many recent jquery libs and jquery itself do that) Commented Jan 23, 2017 at 15:39

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.