繁体   English   中英

使用带有Backbone和Requirejs的jQuery插件

[英]Use jQuery plugin with Backbone and Requirejs

我正在使用backbone + requirejs + jquery,我在当前的html页面(精确的主干html模板)中加载了jquery插件有问题。

有我的要求配置:

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页面中,我有:

<script type='text/javascript' src='/js/jquery/jquery.camera.js'></script>

在我的模板页面中,我有:

<script type="text/javascript">
  jQuery(function() {

    jQuery('#test').camera({
...
</script>

最后我的浏览器消息:

Uncaught TypeError: Object [object Object] has no method 'camera'

你有什么主意吗?

同时我还有另一个问题,在我们当前页面中包含一些js代码的最佳方法是使用backbone,requirejs等。

谢谢 :)

我解决了类似的问题(Jquery.cookie),但我的问题是Jquery正在加载,然后Jquery.cookie被包含但需要已经有JQuery作为静态资源。

所以像这样我将Jquery.Cookie传递给应用程序,它只在我的应用程序范围中插入jquery.cookie。

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类中

require([
  'jquery'
  ,'underscore'
  ,'backbone'
  ,'mapApp'
  ,'jquery.cookie'   //<- this is the real trick !!!
],
  function ($, _, Backbone, App) {

在此之后,我能够找到jquery cookie。

顺便说一句 :如果您使用Require.js来获取它,则无需在您的html中导入JQuery.camera,除非您在Require.js范围之外使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM