0

I am creating a custom js for my module.

app/code/Namespace/Module/view/frontend/requirejs-config.js

var config = {
    map: {
        '*': {
            myjs:'Namespace_Module/js/myjs'
        }
    }
};

app/code/Namespace/Module/view/frontend/web/js/myjs.js

define([
    "jquery",
    "underscore",
    "jquery/ui"
],
    function($, _){
        console.log('1');
        $.widget('mage.myjs', {
            options: {
                divId: '',
                ajaxUrl: '',
                isNeedLast: false,
                autoSubmit: false,
            },
            selects: [],
        _create: function() {
            console.log('inside create'); // this log is not displayed
        }
    });

    return $.mage.myjs;

});

and calling it in the phtml like below :

require(['jquery', 'myjs'], function($) {
        var finderConfig = {"ajaxUrl":"<?php echo $block->getAjaxUrl() ?>","isNeedLast":1,"autoSubmit":1};
        finderConfig.divId = 'finder_<?php echo $finderID ?>';
        $("#finder").myjs(finderConfig);
    });

here the console.log('1') is displayed but the console.log('inside create') inside _create function is not displayed.

cany anyone point out what i am missing.Thanks.

5
  • check sohelrana09.wordpress.com/2016/02/27/… Commented Apr 19, 2016 at 9:48
  • it is still not working @SohelRana Commented Apr 19, 2016 at 10:15
  • Have you use some code in your phtml file ? Commented Apr 19, 2016 at 10:17
  • @SHPatel Yes, check the edited question. Commented Apr 19, 2016 at 10:23
  • Have you created div with id="finder" in your phtml code ? Commented Apr 19, 2016 at 11:14

2 Answers 2

0

Add below code in your phtml file.

<div id="finder"></div>
<script>
    require(['jquery', 'myjs'], function($) {
        var finderConfig = {"ajaxUrl":"<?php echo $block->getAjaxUrl() ?>","isNeedLast":1,"autoSubmit":1};
        finderConfig.divId = 'finder_<?php echo $finderID ?>';
        $("#finder").myjs(finderConfig);
    });
</script>

I think you are missing div with id="finder" in your code. please check it with this code.

let me know if you have any issue.

1
  • yes ur correct i was accessing the class with # instead of . thanks.! Commented Apr 19, 2016 at 12:10
0

Try it:

app/code/Namespace/Module/view/frontend/web/js/myjs.js

define([
    "jquery",
    "underscore",
    "jquery/ui"
],
function ($) {
    'use strict';
        console.log('1');
        $.widget('mage.myjs', {
            options: {
                divId: '',
                ajaxUrl: '',
                isNeedLast: false,
                autoSubmit: false,
            },
        _create: function() {
            console.log('inside create');
        }
    });
    return $.mage.myjs;
});

and calling it in the phtml like below :

<script type="text/x-magento-init">
    {
        "*": {
            "myjs": {
                    "divId": "finder_" + <?php echo json_encode($finderID) ?>,
                    "ajaxUrl": <?php echo json_encode($block->getAjaxUrl()) ?>,
            }
        }
    }

</script>

Your Answer

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.