1

Im new to the javascript module pattern and can't get something like this to work, am I missing something? The $.bind don't give any errors and dropBox is not NULL.

 var Application = (function(d, w, $) {

    var drop, dragStart, dragEnter, dragOver, dragLeave;

    drop = function(e) {
    };

    dragStart = function(e) {
    };

    dragEnter = function(e) {
    };

    dragOver = function(e) { 
    };

    dragLeave = function(e) {  
    };

    return {

        init: function() {

            var dropBox = $('#someid');
            dropBox.bind('dragstart', dragStart);
            dropBox.bind('dragenter', dragEnter);
            dropBox.bind('dragover', dragOver);
            dropBox.bind('drop', drop);
            dropBox.bind('dragleave', dragLeave);
        }
    };

})(document, window, window.jQuery);
4

1 回答 1

0

(function(...) {})(...);您只需调用已声明的函数,该函数返回一个带有成员init()的对象。只要不调用init()成员函数,什么都不会发生。

您应该调用Application.init()文档就绪事件:

$(function() {
    Application.init();
});

如果你调用它太早,$('#someid')将是空的。

除此之外,您问题中的代码没有任何问题。看到这个jsFiddle

于 2013-08-20T11:22:45.550 回答