Javascript 自执行匿名函数的独特实例
我已经创建了模块化 AJAX/PHP 框架的 PHP 端,现在我正在尝试实现客户端。
根据我之前使用模块化 Web 应用程序的经验,我知道有时需要一个特定模块的多个实例。例如,基于网络的两人游戏,每个用户都有页面部分。
在 PHP 方面,我为模块的每个构造实例分配了一个唯一的 ID,我可以将此 UID 传递给浏览器,但我不知道如何实现该模块实例的 Javascript 方面。
模块可以一次性全部加载,也可以通过 AJAX 单独加载(我使用的是 jQuery)。
现在我正在使用我在一些文章中找到的模块化方法,但如果这有助于解决这个问题而不牺牲模块化和私有/公共代码分离,我可以以其他方式重新设计它。现在假设我有一个包含以下内容的 js 文件:
//Self-Executing Anonymous Func
(function( MyModule, $, undefined ) {
// My Uid
MyModule.UID = "";
//Public Method
MyModule.onLoad = function() {
alert("Hey, you loaded an instance of MyModule with UID " + MyModule.UID);
};
//Private Methods follow
function somethingPrivate( ) {
}
}( window.MyModule = window.MyModule|| {}, jQuery ));
我正在使用 Smarty 作为模板。比方说,我有一个像这样的简单模块模板:
<div id="{$contents.moduleuid}">
here goes the contents of the module which can be accessed from MyModule Javascript code by using this unique moduleuid
</div>
我已经设置了服务器端,因此每个模块都会自动使用 Javascript 附加附加模板:
<script type="text/javascript">
/*
TODO: here I have access to the {$contents.moduleuid}
But I have no idea what to put here to create a unique instance of MyModule
(also it might need loading js file if it was not loaded yet) and I should also set for
this instance MyModule.UID to {$contents.moduleuid}
and also call MyModule.onLoad for this instance after it has loaded its Javascript.
*/
</script>
我对高级 Javascript 主题没有经验,所以我不清楚如何创建单独的实例在服务器端构建的每个模块的 MyModule ?是否有可能创建自执行匿名函数的实例?如果没有,那么我如何使用分离的私有/公共代码来实现和克隆 Javascript 对象?
I have created the PHP side of a modular AJAX/PHP framework and now I am trying to implement the client side.
From my previous experience with modular web applications I know that sometimes multiple instances of one particular module are needed. For example, a web based two player game with page parts for each user.
On PHP side I have assigned a unque ID to each constructed instance of the module and I can pass this UID to the browser but I have no idea how to implement the Javascript side of this module instance.
Modules can be loaded all in one go or loaded separately through AJAX (I am using jQuery).
Now I am using a modular approach that I found in some article, but I can redesign it in some other way if that would help to solve this issue without sacrifising modularity and private/public code separation. For now let's say I have a js file with the following:
//Self-Executing Anonymous Func
(function( MyModule, $, undefined ) {
// My Uid
MyModule.UID = "";
//Public Method
MyModule.onLoad = function() {
alert("Hey, you loaded an instance of MyModule with UID " + MyModule.UID);
};
//Private Methods follow
function somethingPrivate( ) {
}
}( window.MyModule = window.MyModule|| {}, jQuery ));
I am using Smarty for templates. Let's say, I have a simple module template like this:
<div id="{$contents.moduleuid}">
here goes the contents of the module which can be accessed from MyModule Javascript code by using this unique moduleuid
</div>
I have set up the server side so each module automatically appends additional template with Javascript:
<script type="text/javascript">
/*
TODO: here I have access to the {$contents.moduleuid}
But I have no idea what to put here to create a unique instance of MyModule
(also it might need loading js file if it was not loaded yet) and I should also set for
this instance MyModule.UID to {$contents.moduleuid}
and also call MyModule.onLoad for this instance after it has loaded its Javascript.
*/
</script>
I am not experienced with advanced Javascript topics so it is unclear to me how I can create a separate instance of MyModule for each module which gets construced server-side? Is it possible at all to create instances of self-executing anonymous functions? If not, then how can I implement and clone Javascript objects with separated private/public code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的建议是保持客户端和服务器端松散耦合。尝试完全使用 HTML/JS 构建模块化客户端应用程序,而不使用 PHP 技巧。据我了解,每个模块(或 UI 组件)都需要与其他模块松散耦合。在这种情况下,您可能需要考虑其他几个问题:
如果您的客户端是一个大型且复杂的应用程序,您可能需要寻找其他问题,例如 JS 优化、单元测试、文档、产品子模块等。
请查看我们在 < 中提出的 BoilerplateJS Javascript 参考架构a href="http://boilerplatejs.org" rel="nofollow noreferrer">http://boilerplatejs.org。它提出了解决我上面讨论的所有问题的方法。
My recommendation is to keep the client side and server side loosely coupled. Try to build your modular client application completely with HTML/JS without PHP tricks on it. As I understand, each of your module (or UI component) need to be loosely coupled from the others. In such case there are several other concerns you might need to look for:
If your client is a large and complex application, you might need to look for other concerns such as JS optimization, unit testing, documentation, product sub modules, etc.
Have a look at the BoilerplateJS Javascript reference architecture we put forward at http://boilerplatejs.org. It suggests ways to address all concerns I discussed above.
由于您已经在使用 jQuery,因此您可以创建一个 jQuery 插件。该插件应该按照您需要的方式运行,我相信您甚至不需要唯一的 ID。考虑到每个模块的实例都包含在带有
module-container
类的 div 中,用于向 div 添加客户端行为的 jQuery 代码将如下所示:最小的插件代码将是(考虑到它在一个单独的 .js 文件中):
如果您使用 jQueryUI,我还建议您也查看“小部件工厂”(简介,docs),它作为构建强大的标准化 jQuery 插件的基础。
Since you are already using jQuery, you could create a jQuery plugin. The plugin should behave the way you need, and I believe you won't even need a unique ID. Considering each of your module's instance is contained in a div with class
module-container
, your jQuery code for adding client-side behavior to the divs would be something like this:The minimal plugin code would be (considering it's in a separate .js file):
If you are using jQueryUI, I also recommend you also look into the "widget factory" (intro, docs), which serves as a base for building powerful, normalized jQuery plugins.