Backbonejs 应用程序的 Requirejs
我想使用 Requirejs 来运行 Backbonejs 应用程序。这是我的基本设置。
根/
根目录/index.htm
根目录/脚本/
根/脚本/require-jquery.js
根/脚本/order.js
根/脚本/main.js
根目录/scripts/app.js
根/脚本/app.build.js
root/scripts/backbone.js
index.htm
<script data-main="scripts/main" src="scripts/require-jquery.js"></script>
<div id="home"></div>
main.js
require([
"order!http://documentcloud.github.com/underscore/underscore.js",
"order!backbone",
"order!app"
],function(_,Backbone,app){
app.init();
});
app.js
define(["jquery","underscore"],function($,_){
var init = function(){
var arr = ['orange','apple','bannana'];
_.each(arr,function(fruit){
console.log(fruit);
});
};
return { init: init };
});
backbone.js
define(["order!http://documentcloud.github.com/backbone/backbone.js"],
function(){
return Backbone;
});
我没有还没有进入 Backbone 的内容,因为我在当前设置中遇到了错误...
第 150 行:_ 在 _.extend(Backbone.Model.prototype, Backbone.Events, {) 处未定义
我我试图让这个东西尽可能简单,并能够最终添加我的路由器、模型等......并在路上构建这个东西......
来让这个东西摇摆?
我在我的设置中缺少什么 ,使用本地 js 文件而不是尝试从 CDN 加载内容会更好吗?
I want to use Requirejs to run a Backbonejs application. Here is my basic setup.
root/
root/index.htm
root/scripts/
root/scripts/require-jquery.js
root/scripts/order.js
root/scripts/main.js
root/scripts/app.js
root/scripts/app.build.js
root/scripts/backbone.js
index.htm
<script data-main="scripts/main" src="scripts/require-jquery.js"></script>
<div id="home"></div>
main.js
require([
"order!http://documentcloud.github.com/underscore/underscore.js",
"order!backbone",
"order!app"
],function(_,Backbone,app){
app.init();
});
app.js
define(["jquery","underscore"],function($,_){
var init = function(){
var arr = ['orange','apple','bannana'];
_.each(arr,function(fruit){
console.log(fruit);
});
};
return { init: init };
});
backbone.js
define(["order!http://documentcloud.github.com/backbone/backbone.js"],
function(){
return Backbone;
});
I haven't gotten to the Backbone stuff yet because I am running into an error with this current setup...
Line 150: _ is undefined at _.extend(Backbone.Model.prototype, Backbone.Events, {
I am trying to make this thing as simple as possible and be able to eventually add my routers, models, etc...And build the thing down the road...
What am I missing here in my setup to get this thing rocking?
Also, would it be better in any way to use local js files instead of trying to load things from CDNs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下内容可能会有所帮助:
Requirejs 的顺序确实不适用于优先级配置和 CDN 依赖项
简而言之,require 不能与 CDN 资产和 order.js 完美配合——您必须嵌套您的 require 调用(这有点糟糕)。
通常我使用主干和下划线的本地副本,它们与
order!
插件配合得很好The following might be helpful:
Requirejs' order does not work with priority config and CDN dependencies
In short, require doesn't work perfectly with CDN assets and order.js--you have to nest your require calls (which kinda stinks).
Normally I've used local copies of backbone and underscore and they've worked well with the
order!
plugin在这种情况下,如果您有已知的依赖层次结构,您可能需要尝试使用 requirejs 的
shim
功能。由于您知道主干具有 jQuery 和 Underscore 的依赖关系,因此您可以相应地对主干进行填充。过去,这是我处理这种情况的方法(我还提供了一些示例代码,说明如何通过 CDN 加载依赖项并使用本地回退,如果您选择,也可以使用 Bower 目录)。我建议确保您使用 requirejs 来捆绑您的资源(节省时间并提高性能):
main.js
router.js
In cases like this, where you have a known dependency heirarchy you may want to try using the
shim
functionality of requirejs. Since you know that backbone has dependencies for jQuery and Underscore, you shim backbone accordingly.In the past here's how I've handled this situation (I've also included some example code on how to load your dependencies via CDN with a local fallback, you could use a bower directory as well if you choose). I would recommend making sure you're using requirejs to bundle your resources (saves time and improves performance):
main.js
router.js