How connect jquery.inputmask using requirejs
Asked Answered
P

2

13

How correctly connect https://github.com/RobinHerbots/jquery.inputmask using requirejs? Method on official site documentation not correct.

I try make it like this

require-config.js

paths: {
  ...
  inputmask: "/assets/vendor/jquery.inputmask/dist/inputmask/inputmask",
  dependencyLib: "/assets/vendor/jquery.inputmask/dist/inputmask/dependencyLib",
  jQueryInputmask: "/assets/vendor/jquery.inputmask/dist/inputmask/jquery.inputmask",
  ...
}

script.js

define('script', ['jquery', 'jQueryInputmask'], function ($) {
    ...
    $obj.inputmask(
        {
            alias: 'currency',
            rightAlign: false,
            digits: 0
        }
    );
    ...
});

but it not work

Putout answered 6/10, 2015 at 22:24 Comment(1)
Are you getting any error messages on the console?Adulthood
S
6

Try with this code:

require.config({
    paths: {
        ...
        "jquery"            : "../dist/jquery/jquery",
        "jQueryInputmask"   : "../dist/inputmask/jquery.inputmask",
        "inputmask"         : "../dist/inputmask/inputmask",
        ...
    },
    shim: {
        jquery: {
            exports: "$"
        },
        jQueryInputmask: {
            deps: ["jquery", "inputmask"],
            exports: "$"
        },
)

define('script', ['jquery', 'jQueryInputmask'], function ($) {
    ...
    $obj.inputmask(
        {
            alias: 'currency',
            rightAlign: false,
            digits: 0
        }
    );
    ...
});

In my project it works setting the shim:

shim: Configure the dependencies, exports, and custom initialization for older, traditional "browser globals" scripts that do not use define() to declare the dependencies and set a module value.

Saintjust answered 23/10, 2015 at 18:11 Comment(0)
H
0

I do it this way:

require.config({
    paths: {
        jquery: '//cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min',
        inputmask: '//cdn.jsdelivr.net/npm/[email protected]/dist/min/jquery.inputmask.bundle.min'
    },
    shim: {
        inputmask: {
            deps: ['jquery'],
            exports: 'Inputmask'
        }
    }
});

define('script', ['inputmask'], function (Inputmask) {
    new Inputmask('email').mask('#obj');
});
Herwig answered 14/1, 2018 at 3:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.