0

次のような機能を含む私の部分がありますmain.js

function isTrue(x){...}
function resizeEditors() {...}
function updateLayout() {...}
function prettify() {...}
function setTheme(theme) {...}
function themedLayout(isDark){...}
function enablePanel(panel) {...}
function disablePanel(panel) {...}
function enableDefaultPanels() {...}
function toggleFullscreen() {...}
function toggleEditorFullscreen(selected) {...}

これらの関数をmain.jsファイルの依存関係で利用できるようにする方法はありますか?

たとえば、editors.js私は関数を使用していますが、editors.jsモジュールはファイル内にあるため、isTrue現在見つけることができませんisTruemain.js

editors.setShowPrintMargin( isTrue( settings.showPrintMargin ) );

編集

プロジェクトはどのように見えるか:

main.js

require(['jquery', 'appSession', 'editors'], function ($, appSession, editors) {
    function isTrue(x){...}
    function resizeEditors() {...}
    function updateLayout() {...}
    function prettify() {...}
    function setTheme(theme) {...}
    function themedLayout(isDark){...}
    function enablePanel(panel) {...}
    function disablePanel(panel) {...}
    function enableDefaultPanels() {...}
    function toggleFullscreen() {...}
    function toggleEditorFullscreen(selected) {...}
});

editors.js

define(['jquery', 'appSession'], function($, appSession){
    ...
    editors.setShowPrintMargin( isTrue( settings.showPrintMargin ) );
    ...
    return editors;
});
4

2 に答える 2

2

はい、返品できます。

define(function () {
    return {
        isTrue: function() {
            // Code
        },
        otherFunction: function() {
            // Code
        }
    }
});

次に、これをお尻に使用します

require(["main"], function(main) {

    main.isTrue(false);

});

モジュールの定義について詳しくは、Webサイトを参照してください。

于 2013-03-21T13:01:15.287 に答える
1

共有/グローバル機能を含むモジュールを作成し、それを必要とするモジュールの依存関係にすることができます。

globals.js:

define([], function() {
    function isTrue(x){}
    // rest of functions...
    function toggleEditorFullscreen(selected) {}

    return { // return functions... };
});

次にそれを使用するには:

require(["globals", "editors"], function(globals, editors) {
    // ...
    editors.setShowPrintMargin(globals.isTrue(settings.showPrintMargin));
    // ...
});

または、editorsモジュール内で使用したい場合は、editors.jsは次のようになります。

define(["globals"], function(globals) {
    // ...
    setShowPrintMargin(globals.isTrue(settings.showPrintMargin));
    // ...
});

あるいは、本当にグローバルにしたい場合は、次のことができるはずです。

window.isTrue = function(valueToCheck) {
    // implementation ...
};
于 2013-03-21T13:04:02.543 に答える