【发布时间】:2015-07-23 14:26:30
【问题描述】:
当我使用 gulp 和 browserify 连接 javscript 文件时,我的 javascript 模块无法正常工作。
这是我的模块。
LastPost.js
var PostBox = require('../objects/PostBox');
var LastestPosts = (function() {
var btnLeft;
var btnRight;
var postBoxs = (function() {
var arr = new Array();
for (var i=0; i<4; i++) {
var post_box = new PostBox();
arr.push(post_box);
}
return arr;
})();
var index = 0;
var lastIndex;
return({
initDOM: function($) {
btnLeft = $('#btn-lastest-previous');
btnRight = $('#btn-lastest-next');
/* some codes */
},
initIndex: function() {
if (index == 0) {
/* some codes */
}
else if (index == lastIndex) {
/* some codes */
}
else {
/* some codes */
}
},
getLastIndex: function() {
$.get('/lastestposts-getlastindex', function(data) {
lastIndex = data;
});
},
updatePostBox: function() {
var parameter = { index: index };
$.get('/lastestposts-getindex', parameter, function(data) {
/* some codes */
});
},
increaseIndex: function() {
if (index < lastIndex) {
index++;
}
},
decreaseIndex: function() {
if (index > 0) {
index--;
}
},
getLeftBtn: function() {
return btnLeft;
},
getRightBtn: function() {
return btnRight;
}
});
})();
module.exports = LastestPosts;
Main.js
var LastestPosts = require('./module/LastestPosts');
$(document).ready(function() {
LastestPosts.initDOM($);
LastestPosts.getLastIndex();
LastestPosts.updatePostBox();
LastestPosts.initIndex();
(LastestPosts.getRightBtn()).click(function() {
LastestPosts.increaseIndex();
LastestPosts.initIndex();
LastestPosts.updatePostBox();
});
(LastestPosts.getLeftBtn()).click(function() {
LastestPosts.decreaseIndex();
LastestPosts.initIndex();
LastestPosts.updatePostBox();
});
});
btnLeft 和 btnRight 可以正常点击,但 LastestPosts.increaseIndex() 和 LastestPosts.initIndex() 无法正常工作。
在使用 gulp 和 browserify 之前,我会像这样导入每个 javascript 文件。
<script src="./js/objects/PostBox.js"></script>
<script src="./js/module/Lastestposts.js"></script>
<script src="./js/main.js"></script>
这很好用,但是在我使用 gulp 和 browserify 连接 javascript 文件后,我的 javascript 模块不起作用...
这是我的 gulp 设置。
gulp.task('build-js', function () {
return browserify('./public/src/js/main.js')
.bundle()
.pipe(source('bundle.js'))
.pipe(gulp.dest('./public/dist/js'));
});
请帮助这个小学生,并为我糟糕的英语水平感到抱歉。我英语不好:'(
【问题讨论】:
-
好像我缺少一些代码。你在某个地方的回购中拥有一切吗?我会在第二天左右看看。
-
非常感谢 :) 你救了我。这是 git 仓库。 github.com/seokju-na/blog-bang-extend.git
标签: javascript node.js module gulp browserify