Backbone AMD with requireJs
Backbone AMD with requireJs
从日志中,主干现在注册AMD (Require.js)自v1.1.1。
很好,所以我试着为一个模块做同样的事情,但有一些我不明白。
如果我们看一下注释源的第4节,该模块没有返回全局Backbone。
不需要垫片和窗户。骨干网可用。但是为什么主干不能被定义呢?
//定义主干模块
define(['underscore', 'jquery', 'exports'], function(_, $, exports) {
root.Backbone = factory(root, exports, _, $);
});
//要求主干模块
require(['backbone'], function (Backbone) {
Backbone // is not undefined ?
});
//模态模块定义
define(['jquery'], function ($) {
root.Modal = factory(root, {}, $);
});
//要求Modal模块
require(['modal'], function (Modal) {
Modal // undefined
});
进入我的模块(使用相同的结构),当我需要我的模块时,如果我不返回任何东西,我得到undefined
。
必须返回根。模态,以使其工作。return root.Modal = factory(root, {}, $);
主要问题是"如何在模块不返回任何东西的情况下需要模块? "
我在requireJS上丢失了一些东西,但是我没有找到它。
http://backbonejs.org/docs/backbone.html第四节
所讨论的代码是:
define(['underscore', 'jquery', 'exports'], function(_, $, exports) {
root.Backbone = factory(root, exports, _, $);
});
注意现在factory
被调用,exports
作为第二个参数。工厂函数将通过在这个对象上设置字段来导出Backbone的方法。这就是为什么当你需要Backbone时,你会得到一个有用的值,而不是undefined
或垃圾。
这个工作的原因是RequireJS支持通过从你给define
的工厂函数返回一个值来定义一个模块,所以你可以这样做:
define(function () {
return {
foo: function () { ... }
};
});
但是它也支持其他方式,例如:
define(['exports'], function (exports) {
exports.foo = function () { ... };
});
在上面的代码中,在依赖列表中名为exports
的模块是一个特殊的(并且保留的)模块名,意思是"给我一个对象,我可以在其上设置字段以导出值"。Backbone使用第二种方式导出值。
相关文章:
- Fighting with FRP
- issue with FB.Event.subscribe
- 以可优化的方式使用requirejs加载模板
- RequireJS向模块传递配置总是返回undefined
- geolocation-marker.js conflict with markerclusterer.js
- Angular 2.0 with JavaScript or TypeScript?
- 为什么不推荐使用“with”?是否有更好或其他方法可以“下降”到对象的命名空间
- RequireJS with Angularjs 用于在同一页面上加载控制器
- 如何使用 jquery.validate with requirejs.
- Using moment.twitter.js with requirejs
- Backbonejs/Undercorejs with RequireJS-渲染后视图未重新加载Jquery
- RequireJS with jQuery Validation
- RequireJs with moment.js不会触发' init '
- Setting up jQuery noConflict with RequireJS
- Load jQuery with RequireJS
- Backbone AMD with requireJs
- Gruntfile issue with grunt-contrib-requirejs
- RequireJS with CommonJS modules
- Requirejs导出数组reset with [] literal不再被导出
- 在RequireJS中对define with dependencies感到困惑