Browserify包未定义的依赖项

时间:2015-05-05 12:33:47

标签: javascript backbone.js browserify browserify-shim

我在browserify项目中使用Backbone并填充owlCarousel 因为它不是commonJS类似的模块。 shim转换似乎有效,但在我真正需要的时候却没有。

我的app.js文件如下:

var settingsView = require("./settingsView.js");

new settingsView();

和我的settingsView

var app = require("./controllers.js"),
    $ = require("jQuery"),
    Backbone = require("Backbone"),
    owlCarousel = require("owlCarousel");
    Backbone.$ = $;

module.exports = Backbone.View.extend({
   el: ".settings",

   events: {
    "click .carousel-img": "setBackground",
    "click .palette div": "setTextColor"
   },

   options: {
    singleItem: true,
    pagination: false,
    navigation: true,
    mouseDrag: false
   },

   initialize: function () {
     this.initCarousel();
   },

   initCarousel: function () {
     /* This is causing the problem */
      this.owlCarousel = this.$el.find(".carousel").owlCarousel(this.options);
   }
})

我的package.json

"browser": {
    "jquery": "./node_modules/jquery/dist/jquery.js",
    "owlCarousel": "./vendor/owl-carousel/owl-carousel/owl.carousel.js"
   },
   "browserify": {
    "transform": [
      "browserify-shim"
    ]
   },
   "browserify-shim": {
    "jquery": "$",
    "owlCarousel": {
      "depends": ["jquery:$"]
    }
   }

它给我的错误:

  

Uncaught TypeError:this。$ el.find(...)。owlCarousel不是函数

在运行时。但是,如果我在dom准备就绪后在控制台中运行$("div").owlCarousel,它就会起作用。

1 个答案:

答案 0 :(得分:1)

我解决了

var app = require("./controllers.js"),
    $ = require("jQuery"),
    Backbone = require("Backbone"),
    owlCarousel = require("owlCarousel");
    Backbone.$ = $;    
    $.fn.owlCarousel = owlCarousel