救命啊!
我非常困惑啊... 我不知道自己在干什么。
我昨天和今天一整天都在看RequireJS和AMD的教程和示例,到了这个地步,可是我觉得我还是对module(模块)有基本的误解。
- 我有一堆从HTML元素中被“onClick”调用的函数...
- 我该如何使用RequireJS定义我的函数,以便我能调用它们?太困惑了 :/ 而且我不懂
- 如何让我的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>
我需要做像这样的事情吗:
//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(); ?
或许可以类似于以下内容:
//function that get's called on body onload!
define('loadPage', function loadPage()
{
return function()
{
hideAllDivs();
//more jquery stuff...
createModelMenu();
//more jquery stuff...
}
});
或者
function(loadPage)?
我看了这些类似的问题:
这些也很有帮助,但还不够:
- http://hippieitgeek.blogspot.se/2013/07/load-jquery-ui-with-requirejs.html
- http://www.requirejs.org/jqueryui-amd/example/webapp/app.html
- https://github.com/jrburke/jqueryui-amd
我尝试将函数分离到另一个文件中,因此我有"index.html"和"Logic.js"...这是要点:
=========================================