javascriptbackbone.jsbrowserifybrowserify-shim

Browserify bundle undefined dependency


I'm using browserify in a Backbone project and shimming owlCarousel because it is not a commonJS like module. The shim transformation seems to work but not when I really need it.

My app.js file looks like:

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

new settingsView();

and my 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);
   }
})

where my 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:$"]
    }
   }

The error it's giving me:

Uncaught TypeError: this.$el.find(...).owlCarousel is not a function

at runtime. But, if I run $("div").owlCarousel in the console after dom ready, it works.


Solution

  • I solved doing

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