基本RequireJS帮助 - 如何调用/定义函数?使用onclick和jquery也是如此

Kat*_*tie 13 javascript jquery amd requirejs js-amd

救命!

我是超级迷茫的家伙......我不知道我在做什么

昨天和今天我一直在关注RequireJS和AMD教程和示例,并且到了这一点,但是我认为我仍然对模块什么有一个根本的误解.

  • 我有一堆函数从我的HTML元素中被称为"onClick"...
    1. 如何使用RequireJS 定义我的函数以便我可以调用它们?很困惑:/我也不明白
    2. 如何让我的onLoad函数被调用(在我的情况下是$(function(),但是如何在RequireJS中启动它?)

我使用的是Node v0.10.12

<html>
...
<head>
<script data-main="" src="libraries/require.js"></script>
...
<script>
...
     //I really need all these javascript files for every function defined on this page...
     require(['simulatorConfiguration.js', 
              'modelConfiguration.js', 
              'libraries/jquery-1.10.2.min.js', 
              'libraries/jquery.lightbox_me.js', 
              'libraries/jquery-migrate-1.2.1.js', 
              'libraries/raphael-min.js'], function(start) { 

            $(function() {

                loadPage();  //<--- CALL LOAD PAGE, but it can't find the function
                //do some jquery stuff
            });

        });

    //function that get's called on body onload!
    define('loadPage', function loadPage()
    {
        hideAllDivs();
        //more jquery stuff...

        createModelMenu();
        //more jquery stuff...
    });

    define('hideAllDivs', function hideAllDivs()
    {
        //some UI stuff...

    });

    define('createModelMenu', function createModelMenu()
    {
        //some jquery stuff...
    });

    define('createTestMenu', function createTestMenu(model_key)
        {
        var javascriptLoc = "models/" + models[model_key].modelDir + "/testConfiguration.js";
        require([javascriptLoc], function(util) {

            showModelInformation(model_key);
            //some Jquery stuff...
        });
        });

    define('showModelInformation', function showModelInformation(model_key)
    {
        hideAllDivs();
        //some jquery stuff
    });

    define('showTest', function showTest(test_key)
    {       
        hideAllDivs();
        //some RaphaelJS stuff...
    });


    define('takeControl', function takeControl()
    {
        //some UI stuff
    });

    define('giveUpControl', function giveUpControl()
    {
        //some UI stuff...

    });
</script>
</head>
<body>
...
<li><a href="#" id="AoD" onclick="showTests(this.id)">Audio On Demand</a></li>
...
<input type="submit" value="Yes, Release Control" onclick="giveUpControl()">
....
<input type="submit" value="Take Control" onclick="takeControl()">
....
</body>
Run Code Online (Sandbox Code Playgroud)

我是否需要做以下事情:

//function that get's called on body onload!
define('loadPage', function loadPage()
{
    return function(loadPage)
        {
            hideAllDivs();
            //more jquery stuff...

            createModelMenu();
            //more jquery stuff...
        }
});
//and call it with loadPage.loadPage(); ?
Run Code Online (Sandbox Code Playgroud)

或者类似的东西:

//function that get's called on body onload!
define('loadPage', function loadPage()
{
    return function()
        {
            hideAllDivs();
            //more jquery stuff...

            createModelMenu();
            //more jquery stuff...
        }
});
Run Code Online (Sandbox Code Playgroud)

要么

function(loadPage)?
Run Code Online (Sandbox Code Playgroud)

我确实看过这些类似的问题:

这些也很有用,但还没有:

我尝试将函数分成另一个文件,所以我有"index.html"和"Logic.js"...这里是要点:

=========================================

https://gist.github.com/anonymous/6470443

Chr*_*ris 5

(a)创建和(b)加载模块所需的最小代码如下所示:

// (a) Create two modules, 'hideAllDivs' and 'loadPage'.
define ('hideAllDivs', [], function () {
    return function() {
    };
});

define('loadPage', ['hideAllDivs'], function(hideAllDivs)
{
    return function()
        {
            hideAllDivs();
            //more jquery stuff...

            createModelMenu();
            //more jquery stuff...
        };
});

// (b) Load the loadPage module and call it.
require(['jquery-blah-blah', 'loadPage', 'anotherModule'], function($, loadPage, anotherModule) {
    $(function() {
        loadPage();
    });
});
Run Code Online (Sandbox Code Playgroud)

强烈推荐阅读:http://requirejs.org/docs/api.html#define