使用jQuery插件与骨干网和Requirejs [英] Use jQuery plugin with Backbone and Requirejs
问题描述
我和骨干+ requirejs + jQuery的工作,我有一个jQuery插件加载一个问题,我目前的html页面(骨干HTML模板precisly)。
I'm working with backbone+requirejs+jquery and I have a problem with jquery plugin loading in my current html page (backbone html template precisly).
有我需要的配置:
require.config({
paths: {
// ... some code about backbone config
jquery: '/js/lib/jquery/jquery.min',
'jquery.camera' : '/js/jquery/jquery.camera'
},
shim: {
// ... some code about backbone config
'jquery.camera': ['jquery']
}
});
在我的布局html页面我有:
In my layout html page I have:
<script type='text/javascript' src='/js/jquery/jquery.camera.js'></script>
在我的模板页面我有:
and in my template page I have:
<script type="text/javascript">
jQuery(function() {
jQuery('#test').camera({
...
</script>
最后我的浏览器MESG:
Finally my browser mesg :
Uncaught TypeError: Object [object Object] has no method 'camera'
你有什么想法?
在同一时间,我还有一个问题,什么是有一些JS code在我国目前与骨干,requirejs等网页的最佳方式。
In the same time I have another question, what is the best way to include some js code in our current page with backbone,requirejs, etc.
感谢:)
推荐答案
我解决了类似的问题(Jquery.cookie)这样的,但我的问题是:jQuery是加载,然后被纳入Jquery.cookie但需要已经有JQuery的一个静态的资源。
I solved a similar issue (Jquery.cookie) like this, but my problem was that Jquery was being loaded and then Jquery.cookie was being included but require already had JQuery as a Static resource.
所以这样我通过Jquery.Cookie的应用程序,它在我的应用范围插入jquery.cookie只。
So like this I pass Jquery.Cookie to the application and it inserts jquery.cookie in my application scope only.
require.config({
paths: {
'async' : 'libs/async'
,'jquery' : 'libs/jquery'
,'underscore' : 'libs/underscore'
,'backbone' : 'libs/backbone'
,'text' : 'libs/text'
,'jquery.cookie' : 'libs/jquery.cookie' // <-- cookie lives here
}
,shim: {
'jquery': {
exports: '$'
}
,'underscore': {
exports: '_'
}
,'backbone': {
deps: ['underscore', 'jquery'],
exports: 'Backbone'
}
,'jquery.cookie': { //<-- cookie depends on Jquery and exports nothing
deps: ['jquery']
}
}
});
,然后在我加入了主要的App类
and then in the main App class I added
require([
'jquery'
,'underscore'
,'backbone'
,'mapApp'
,'jquery.cookie' //<- this is the real trick !!!
],
function ($, _, Backbone, App) {
这之后,我能找到的jQuery的cookie。
After this I was able to find jquery cookie.
BTW :有没有必要进口JQuery.camera在你的HTML,如果你使用的是Require.js获取它,除非你使用它的Require.js范围之外
BTW: there is no need to import JQuery.camera in your html if you are using Require.js to fetch it, unless you use it outside your Require.js scope.
这篇关于使用jQuery插件与骨干网和Requirejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!