Javascript 模块模式和 Jquery 上线了吗?

发布于 2024-10-19 12:47:29 字数 2235 浏览 6 评论 0原文

我正在尝试使用 JavaScript 模块模式 并且我遇到一个我不确定如何解决的问题。

所以我有 2 个脚本文件,因为我想分隔我的代码并使其更易于阅读。

// script 1

var abc = (function (my, $)
{
    my.events = function ()
   {
        // selectors is from my base file(not shown as I don't think it is needed to be shown)
        // my.selectors.createFrm = '#createFrm'
        var createSubmitFrmHandler = $(my.selectors.createFrm).live('submit', function (e)
        {
            e.preventDefault();
        });

   }

   return my;

} abc || {}, jQuery));

// script 2

var abc = (function (my, $)
{
     my.dialogs = {

        addDialog: function ()
        {
            var $dialog = $('<div></div>').dialog(
            {
                width: 580,
                height: 410,
                resizable: false,
                modal: true,
                autoOpen: false,
                title: 'Basic Dialog',
                buttons:
                    {
                        Cancel: function ()
                        {
                            $(this).dialog('close');
                        },
                        'Create': function ()
                        {

                            jQuery.validator.unobtrusive.parse(my.selectors.createFrm)
                            // this is undefined as page loadup no form was found so live did not kick in
                            my.createSubmitFrmHandler.validate().form();

                        }
                    }
            });

            return $dialog;
        },

    return my;
} abc || {}, jQuery));

所以我不确定如何确保定义了 createSubmitFrmHandler 并继续我正在做的事情。

编辑

我最终做了这样的事情

   var abc = (function (my, $)
    {
        my.events = function ()
       {
            // some one time events here
       }

        my.test = function() 
        {
            var add = $(selectors.createFrm).live('submit', function (e)
            {
                e.preventDefault();
            });

            return add;
        };
    }

我唯一不确定的是,如果我一遍又一遍地调用这个函数,它会继续创建这个对象,还是会看到live已经绑定了并且不再进行任何绑定?

I am trying to use the JavaScript Module Pattern and I run into a problem that I am unsure how to get around.

So I have 2 script files as I want to separate my code and make it easier to read.

// script 1

var abc = (function (my, $)
{
    my.events = function ()
   {
        // selectors is from my base file(not shown as I don't think it is needed to be shown)
        // my.selectors.createFrm = '#createFrm'
        var createSubmitFrmHandler = $(my.selectors.createFrm).live('submit', function (e)
        {
            e.preventDefault();
        });

   }

   return my;

} abc || {}, jQuery));

// script 2

var abc = (function (my, $)
{
     my.dialogs = {

        addDialog: function ()
        {
            var $dialog = $('<div></div>').dialog(
            {
                width: 580,
                height: 410,
                resizable: false,
                modal: true,
                autoOpen: false,
                title: 'Basic Dialog',
                buttons:
                    {
                        Cancel: function ()
                        {
                            $(this).dialog('close');
                        },
                        'Create': function ()
                        {

                            jQuery.validator.unobtrusive.parse(my.selectors.createFrm)
                            // this is undefined as page loadup no form was found so live did not kick in
                            my.createSubmitFrmHandler.validate().form();

                        }
                    }
            });

            return $dialog;
        },

    return my;
} abc || {}, jQuery));

So I am not sure how to make sure that createSubmitFrmHandler is defined and continue what I am doing.

Edit

I ended up doing something like this

   var abc = (function (my, $)
    {
        my.events = function ()
       {
            // some one time events here
       }

        my.test = function() 
        {
            var add = $(selectors.createFrm).live('submit', function (e)
            {
                e.preventDefault();
            });

            return add;
        };
    }

The only thing I am unsure is that if I call up this function over and over will it keep making this object or will it look and see that the live is already bound and won't do any more binding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云醉月微眠 2024-10-26 12:47:29

模块模式的要点是 Javascript 具有函数作用域:用 var 定义的变量对于定义它们的函数来说是本地的。

(function() {
    var foo = 'bar';
    // foo == 'bar'
})();
// foo == undefined

由于您在分配给 my.events 的函数中定义了 createSubmitFrmHandler,因此您无法在该函数主体之外引用它。有几种方法可以解决这个问题。将 my 传递给所有模块的目的是它们可以通过它共享机密:您可以在第一个模块中设置 my.events.handler = createSubmitFrmHandler ,并且 my.events.handler 将在另一个模块中可见,因为 my 在那里可见。您可以让 my.events() 返回 createSubmitFrmHandler 并以这种方式引用它。如果选择器不是一个昂贵的选择器,您可以简单地再次计算createSubmitFrmHandler的值,并使用$(my.selectors.createFrm).validate().form(); code> 在对话框模块中,而不是尝试引用 createSubmitFrmHandler。无论你适合什么。

The point of the module pattern is that Javascript has function scope: variables defined with var are local to the function in which they are defined.

(function() {
    var foo = 'bar';
    // foo == 'bar'
})();
// foo == undefined

Since you define createSubmitFrmHandler in the function which you assign to my.events, you cannot refer to it outside the body of that function. There are several ways around this. The point of passing my to all modules is that they can share secrets through it: you can set my.events.handler = createSubmitFrmHandler in the first module, and my.events.handler will be visible in the other module since my is visible there. You could have my.events() return createSubmitFrmHandler and reference it that way. If the selector is not a costly one, you can simply calculate the value of createSubmitFrmHandler again, and use $(my.selectors.createFrm).validate().form(); in the dialog module instead of trying to reference createSubmitFrmHandler. Whatever suits you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文