いつでも任意のファイルを呼び出してその関数を呼び出すことができることを理解しています (間違っている場合は修正してください)。2 つのモジュール ( oneとtwo ) を作成し、 twoにネストされた関数をoneから呼び出すことで、これをテストすることにしました。これは機能しますが、関数twoから関数oneを呼び出すことができません。
Main.js
requirejs.config({
baseUrl: "js"
});
require([
"script/one"
], function ($, $Ui, hBs, one) {
//works fine
one.get("I am the initiator");
});
one.js
define(['script/two'], function (two) {
var one = function () {
return {
get: function (msg) {
//works
console.log("get: " + msg);
//works fine
two.serve1("I am from one of one");
},
post: function (msg) {
// i am calling this method from two.js
console.log("post: " + msg);
two.serve2("i am from two of two");
}
}
}
return new one;
})
two.js
define([ 'require', 'script/one'], function (require,one) {
var two = function () {
one = require('script/one'); // throwing error as "Uncaught Error: Module name "script/one" has not been loaded yet for context: _"
return {
serve1: function (msg) {
console.log("2 in serve1 :" + msg)
// calling doesn't
one.post("initiated from one, called in two");
// throws "Uncaught TypeError: Cannot call method 'post' of undefined"
},
serve2: function (msg) {
console.log("2 in serve2 :" + msg)
}
}
}
return new two;
})
このエラーが発生するのはなぜですか?