简体   繁体   English

Javascript 自执行匿名函数的唯一实例

[英]Unique instances of Javascript self-executing anonymous functions

I have created the PHP side of a modular AJAX/PHP framework and now I am trying to implement the client side.我已经创建了模块化 AJAX/PHP 框架的 PHP 端,现在我正在尝试实现客户端。

From my previous experience with modular web applications I know that sometimes multiple instances of one particular module are needed.根据我之前使用模块化 web 应用程序的经验,我知道有时需要一个特定模块的多个实例。 For example, a web based two player game with page parts for each user.例如,基于 web 的两人游戏,每个用户都有页面部分。

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.在 PHP 端,我为模块的每个构造实例分配了一个唯一 ID,我可以将此 UID 传递给浏览器,但我不知道如何实现此模块实例的 Javascript 端。

Modules can be loaded all in one go or loaded separately through AJAX (I am using jQuery).模块可以全部加载到一个 go 中,也可以通过 AJAX 单独加载(我使用的是 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:现在假设我有一个包含以下内容的 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 ));

I am using Smarty for templates.我正在使用 Smarty 作为模板。 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:我已经设置了服务器端,因此每个模块都会自动附加带有 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?我对高级 Javascript 主题没有经验,所以我不清楚如何为每个在服务器端构建的模块创建一个单独的 MyModule 实例? 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?如果没有,那么我如何使用分离的私有/公共代码实现和克隆 Javascript 对象?

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.尝试完全使用 HTML/JS 构建您的模块化客户端应用程序,而不使用 PHP 技巧。 As I understand, each of your module (or UI component) need to be loosely coupled from the others.据我了解,您的每个模块(或 UI 组件)都需要与其他模块松散耦合。 In such case there are several other concerns you might need to look for:在这种情况下,您可能需要寻找其他几个问题:

  • How to keep your UI component structure (html), presentation (css) and behavior (JS) self contained (for example in a single folder), so that it can live or die independently如何保持您的 UI 组件结构 (html)、表示 (css) 和行为 (JS) 自包含(例如在单个文件夹中),以便它可以独立生存或死亡
  • How these self contained components interact with each other这些自包含的组件如何相互交互
  • How to manage the configurations/settings of your UI components如何管理 UI 组件的配置/设置
  • Should you be using MVVM or MVC pattern to organize and bind the view to your PHP model您是否应该使用 MVVM 或 MVC 模式来组织视图并将其绑定到您的 PHP model
  • Who decides when to create/show/hide your UI components (for example based on URL for bookmarking)谁决定何时创建/显示/隐藏您的 UI 组件(例如,基于 URL 进行书签)

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.如果您的客户端是一个大型且复杂的应用程序,您可能需要寻找其他问题,例如 JS 优化、单元测试、文档、产品子模块等。

Have a look at the BoilerplateJS Javascript reference architecture we put forward at http://boilerplatejs.org .看看我们在http://boilerplatejs.org提出的 BoilerplateJS Javascript 参考架构。 It suggests ways to address all concerns I discussed above.它提出了解决我上面讨论的所有问题的方法。

Since you are already using jQuery, you could create a jQuery plugin.由于您已经在使用 jQuery,您可以创建一个 jQuery 插件。 The plugin should behave the way you need, and I believe you won't even need a unique ID.该插件应该按照您需要的方式运行,我相信您甚至不需要唯一的 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:考虑到您的每个模块实例都包含在带有 class module-container的 div 中,用于向 div 添加客户端行为的 jQuery 代码将如下所示:

$(function(){
    // DOM content is loaded
    $('.module-container').MyPluginName();
});

The minimal plugin code would be (considering it's in a separate.js file):最小的插件代码将是(考虑到它在一个单独的.js 文件中):

(function($){
  $.fn.MyPluginName = function() {  
     // Return this.each to maintain chainability
    return this.each(function() {
      // Keep a reference to your unique div instance.
      var $this = $(this);
      // Plugin logic here
    });
  };
})(jQuery);

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.如果您使用 jQueryUI,我还建议您查看“widget factory”(介绍文档),它是构建强大、规范化 jQuery 插件的基础。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM