使用requirejs,socketio,b骨干设置jsfiddle [英] Set up jsfiddle with requirejs, socketio, backbone
问题描述
我花了半天的时间来尝试设置 requirejs,socket.io,boneerjs 小提琴,以解决另一个SO问题.
I've spent more than half a day trying to set up a requirejs, socket.io, backbonejs fiddle so as to solve another SO question.
这是我尝试过的.您可以签出我的小提琴 我尝试了几种方法,没有任何运气.
Here is what I tried. You may checkout my fiddle I tried a couple ways without any luck.
- 我已经像这样将脚本加载到了头上.
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.2/require.js"></script>
<script>
requirejs.config({
paths: {
'socket.io': ['https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.5.1/socket.io.js'],
'jquery': ['https//cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js'],
'underscore': ['https//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js'],
'backbone': ['https//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js']
},
shim: {
'backbone': ['underscore']
},
waitSeconds: 3
});
</script>
</head>
<body>
<div id='page'>
<div id='incomingChatMessages'>
</div>
</div>
</body>
</html>
-
这是我尝试将脚本加载到js组件中的方式
Here is how I tried to load the scripts in the js component otherwise
requirejs.config({
paths: {
'socket.io': ['https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.5.1/socket.io.js'],
'jquery': ['https//cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js'],
'underscore': ['https//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js'],
'backbone': ['https//cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min.js']
},
shim: {
'backbone': ['underscore']
},
waitSeconds: 3
});
var io = require('socket.io')
var $, Backbone;
require(['jquery', 'underscore', 'backbone'], function(jq, us, bb) {
$ = jq;
Backbone = bb;
});
Backbone.$ = $;
这是链接,该人是在jsfiddle中使用requirejs加载jquery的但这只能部分解决我的问题
here is a link by someone who is loading jquery using requirejs in a jsfiddle but that only addresses my problem partially
推荐答案
很多错误.
-
您完全不应使用HTML窗口.您可以将所有内容都放在JS窗口中.
You should not use the HTML window at all for this. You can put everything in the JS window.
您不应在路径中放置.js
扩展名.
You should not put .js
extensions in paths.
您无需为代码使用回退,因为paths
中的值应该是字符串而不是数组.
There's no need to use fallbacks for the code you have the the values in paths
should be strings rather than arrays.
您在paths
中的URL中输入了错字.
You had typos in your URLs in paths
.
此行var io = require('socket.io')
无法起作用.这是RequireJS,而不是CommonJS.这样的调用可以在define
调用内进行.
This line var io = require('socket.io')
cannot work. This is RequireJS, not CommonJS. Such a call could work inside a define
call.
主干网已经有一段时间没有使用shim
了.您的shim
没用.
Backbone has not needed a shim
for quite a while now. Your shim
for it is useless.
您不需要执行Backbone.$ = $
.
出于历史原因,jQuery和Backbone都将自己泄漏到了全局空间中.因此,您不必手动执行此操作.
For historical reasons both jQuery and Backbone leak themselves into the global space. So you don't have to do this manually.
这是清理过的JS:
requirejs.config({
paths: {
'socket.io': 'https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.5.1/socket.io',
'jquery': 'https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min',
'underscore': 'https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min',
'backbone': 'https://cdnjs.cloudflare.com/ajax/libs/backbone.js/1.3.3/backbone-min'
},
waitSeconds: 3
});
require(['jquery', 'underscore', 'backbone', 'socket.io'], function(jq, us, bb, io) {
console.log($, Backbone.$, Backbone);
});
请注意console.log
如何依赖$
和Backbone
在全局空间中.
Note how the console.log
relies on $
and Backbone
being in the global space.
还有小提琴的叉子.
这篇关于使用requirejs,socketio,b骨干设置jsfiddle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!