Lor*_*eck 10 javascript requirejs
我有我的bootstrap文件,它定义了require.js路径,并加载了app和config模块.
// Filename: bootstrap
// Require.js allows us to configure shortcut alias
// There usage will become more apparent futher along in the tutorial.
require.config({
paths: {
bfwd: 'com/bfwd',
plugins: 'jquery/plugins',
ui: 'jquery/ui',
jquery: 'jquery/jquery.min',
'jquery-ui': 'jquery/jquery-ui.min',
backbone: 'core/backbone.min',
underscore: 'core/underscore.min'
}
});
console.log('loading bootstrap');
require([
// Load our app module and pass it to our definition function
'app',
'config'
], function(App){
// The "app" dependency is passed in as "App"
// Again, the other dependencies passed in are not "AMD" therefore don't pass a parameter to this function
console.log('initializing app');
App.initialize();
});
Run Code Online (Sandbox Code Playgroud)
app.js按照它应该加载,并且它的依赖项被加载.它定义了回调函数,所有正确的依赖项都作为参数传递.不会抛出任何错误.但是,在bootstrap的回调中,App未定义!没有参数传递.是什么导致这个?这是我的app文件(为空间修改)
// Filename: app.js
define(
'app',
[
'jquery',
'underscore',
'backbone',
'jquery-ui',
'bfwd/core',
'plugins/jquery.VistaProgressBar-0.6'
],
function($, _, Backbone){
var initialize = function()
{
//initialize code here
}
return
{
initialize: initialize
};
}
);
Run Code Online (Sandbox Code Playgroud)
pha*_*awk 12
据我所知,您应该只在您的app.js定义方法中删除'app'字符串.
// Filename: app.js
define([
'jquery',
'underscore',
'backbone',
'jquery-ui',
'bfwd/core',
'plugins/jquery.VistaProgressBar-0.6'
], function($, _, Backbone){
...
);
Run Code Online (Sandbox Code Playgroud)
好吧我遇到了同样的问题,关键是你定义的jquery路径别名.事实证明,RequireJS对jquery有一些特殊处理.如果你使用jquery模块名称,它会在那里做一些魔术.
根据你在jquery.min.js中的内容,它可能会导致一些问题,你的jquery插件也可能存在问题.以下是RequireJS源代码的相关代码行:
if (fullName) {
//If module already defined for context, or already loaded,
//then leave. Also leave if jQuery is registering but it does
//not match the desired version number in the config.
if (fullName in defined || loaded[id] === true ||
(fullName === "jquery" && config.jQuery &&
config.jQuery !== callback().fn.jquery)) {
return;
}
//Set specified/loaded here for modules that are also loaded
//as part of a layer, where onScriptLoad is not fired
//for those cases. Do this after the inline define and
//dependency tracing is done.
specified[id] = true;
loaded[id] = true;
//If module is jQuery set up delaying its dom ready listeners.
if (fullName === "jquery" && callback) {
jQueryCheck(callback());
}
}
Run Code Online (Sandbox Code Playgroud)
对我来说,我有一个设置,我有一个名为/libs/jquery/jquery.js的文件,它返回jquery对象(只是RequireJS的包装器).我最终做的只是将路径别名更改jquery为$jquery.这有助于避免不受欢迎的魔术行为.
在原始教程中,我读过他们使用的jQuery也可以.
这是一个可能有助于您入门的简单示例:
我创建了一个非常简单的模块:
https://gist.github.com/c556b6c759b1a41dd99d
define([], function () {
function my_alert (msg) {
alert(msg);
}
return {
"alert": my_alert
};
});
Run Code Online (Sandbox Code Playgroud)
并在这个小提琴中使用它,仅使用 jQuery 作为额外的依赖项:
<script src="http://requirejs.org/docs/release/1.0.7/minified/require.js"></script>
<script type="text/javascript">
require.config({
paths: {
"jquery": "https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min",
"app": "https://gist.github.com/raw/c556b6c759b1a41dd99d/20d0084c9e767835446b46072536103bd5aa8c6b/gistfile1.js"
},
waitSeconds: 40
});
</script>
<div id="message">hello</div>
<script type="text/javascript">
require( ["jquery", "app"],
function ($, app) {
alert($.fn.jquery + "\n" + $("#message").text());
app.alert("hello from app");
}
);
</script>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15026 次 |
| 最近记录: |