我在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
,它就会起作用。
答案 0 :(得分:1)
我解决了
var app = require("./controllers.js"),
$ = require("jQuery"),
Backbone = require("Backbone"),
owlCarousel = require("owlCarousel");
Backbone.$ = $;
$.fn.owlCarousel = owlCarousel