#javascript #node.js #requirejs #require #amd
Вопрос:
У меня есть основной файл JavaScript, содержащий функции и плагины Jquery
my_code1.js:
(function ($) { $.fn.userInfo = function (options) { console.log('$.fn.userInfo start'); $.getJSON("http://jsonplaceholder.typicode.com/posts/", function (data) { $('#result').html('data: ' data); }) .done(function (data) { }) .fail(function (data) { }) .always(function () { }); }; }(jQuery)); function result2(result2) { console.log('function result2 start'); $('#result2').html('result2'); } function result3(result3) { console.log('function result3 start'); $('#result3').html('result3: ' result3); } result4(); result5('result5 ok');
У меня также есть файл с функциями, которые я использую
my_code2.js:
function result4() { console.log('function result4 start'); $.fn.userInfo({}); } function result5(result5) { console.log('function result5 start'); $('#result5').html('result5: ' result5); } result2(); result3('result3 ok');
Теперь я использую Requirejs, и мой код выглядит так
app.js:
requirejs.config({ "baseUrl": "", "paths": { "app": "", "jquery": "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min", "my_code1": "my_code1", "my_code2": "my_code2" }, shim: { 'jquery': { exports: '
When executing this code, I get the error:
Uncaught TypeError: result2 is not a function
In the end, I want to get a code that will connect together all my numerous files in which there are sets of functions.
What should the file app.js for loading my files with my functions look like? How to call functions of another script from script not AMD?
When executing this code, I get the error:
}, 'my_code1': { deps: ['jquery', "my_code2"], }, deps:["jquery"], exports: ["my_code1", 'my_code2'] } }); // document.js file define('global/document', ['global/window'], function (window) { return window.document; }); // window.js file define('global/window', [], function () { return window; }); define('global/document', ['global/window'], function (window) { return window.global; }); requirejs.onError = function (err) { console.log(err.requireType); if (err.requireType === 'timeout') { console.log('modules: ' err.requireModules); } throw err; };
Uncaught TypeError: result2 is not a function
In the end, I want to get a code that will connect together all my numerous files in which there are sets of functions.
What should the file app.js for loading my files with my functions look like? How to call functions of another script from script not AMD?