Javascript 模块模式和 Jquery 上线了吗?
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
模块模式的要点是 Javascript 具有函数作用域:用
var
定义的变量对于定义它们的函数来说是本地的。由于您在分配给
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.Since you define
createSubmitFrmHandler
in the function which you assign tomy.events
, you cannot refer to it outside the body of that function. There are several ways around this. The point of passingmy
to all modules is that they can share secrets through it: you can setmy.events.handler = createSubmitFrmHandler
in the first module, andmy.events.handler
will be visible in the other module sincemy
is visible there. You could havemy.events()
returncreateSubmitFrmHandler
and reference it that way. If the selector is not a costly one, you can simply calculate the value ofcreateSubmitFrmHandler
again, and use$(my.selectors.createFrm).validate().form();
in the dialog module instead of trying to referencecreateSubmitFrmHandler
. Whatever suits you.