当我用gulp和browserify合并javscript文件时,我的javascript模块不起作用。

这是我的模块。

LastestPost.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();
    });
});


btnLeftbtnRight单击得很好,但是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.task('build-js', function () {
    return browserify('./public/src/js/main.js')
        .bundle()
        .pipe(source('bundle.js'))
        .pipe(gulp.dest('./public/dist/js'));
});


请帮助这个小学生,对不起我的英语水平太差了。我的英文说的不是很好 :'(

最佳答案

不知道到底是哪里出了问题,但是问题归结于终端中的此错误-

express deprecated res.send(status): Use res.sendStatus(status) instead app.js:107:9


这意味着最终问题出在此特定函数不返回lastIndex上:

getLastIndex: function() {
    $.get('/lastestposts-getlastindex', function(data) {
        lastIndex = data;
    });
},


如果将res.send(4)修改为res.send({lastIndex: 4});,将getLastIndex()修改为

getLastIndex: function() {
    $.get('/lastestposts-getlastindex', function(data) {
        lastIndex = data.lastIndex;
    });
},


不知道为什么它之前可以工作-但是目前您的lastIndex私有变量是undefined破坏了一切-这就是为什么。

10-07 19:06
查看更多