Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 了解riot.js创建模块化应用程序的功能_Javascript_Riot.js - Fatal编程技术网

Javascript 了解riot.js创建模块化应用程序的功能

Javascript 了解riot.js创建模块化应用程序的功能,javascript,riot.js,Javascript,Riot.js,我了解riot.js是如何触发和处理自定义事件的。我也了解这个库是如何启用模板的。然而,我不理解riot创建和实施所谓“扩展核心的模块”的模式。以下是riot在其极其稀少(也是唯一一篇)的文章中提供的内容: var instance; global.admin = riot.observable(function(arg) { if (!arg) return instance; if ($.isFunction(arg)) { admin.on("read

我了解riot.js是如何触发和处理自定义事件的。我也了解这个库是如何启用模板的。然而,我不理解riot创建和实施所谓“扩展核心的模块”的模式。以下是riot在其极其稀少(也是唯一一篇)的文章中提供的内容:

var instance;

global.admin = riot.observable(function(arg) {

    if (!arg) return instance;

    if ($.isFunction(arg)) {
        admin.on("ready", arg);
    } 
    else {
        instance = new Admin(arg);
        instance.on("ready", function() {
            admin.trigger("ready", instance);
        });
    }
});

这个模式到底是如何工作的,它如何帮助使应用程序核心具有可扩展性?谢谢。

很抱歉延迟接受此邮件。我检查了几天,已经放弃了这方面的任何帮助。感谢您对explanationOne更新的解释-如果管理(配置)纯粹是按顺序进行的,那么您必须手动在实例中触发“就绪”事件。如果代码是连续的,并且事件是在Admin(config)内部触发的,那么instance.on(“ready”,…)将永远不会发生,因为在Admin(config)内部触发事件后,将解释此行。我已经编辑了答案。这个问题可能是针对Riot v1(从问题日期来看)和过期的。Riot v2有可能类似的“混入”。
// Declare variable which will hold application instance.
var instance;

// Define observable admin function to be able to trigger and listen events.
global.admin = riot.observable(function(arg) {

  // Call admin() returns the application instance. 
  if (!arg) return instance;

  /* Call admin(function(arg){...}) will call a function 
     when "ready" event is triggered in admin function 
     passing the application instance as arg to function. 
     In that way presenters are started in riot-admin demo. 
     All models should trigger / listen events on instance 
     and presenters should listen / trigger events on instance (which is observable) 
     thus providing loose coupling of components. 
     Instance itself can play Mediator role */
  if ($.isFunction(arg)) {
    admin.on("ready", arg);
  } 
  else {
    /* Call admin(non_function_argument) is treated as initialization of application 
       with arg being a config object. Admin(arg) returns observable */
    instance = new Admin(arg);

    // Listen to instance's "ready" event. "Ready" is triggered somewhere in Admin(arg).
    instance.on("ready", function() {
      /* Trigger "ready" in admin function to call all functions 
         passed with admin(function(arg){...}) call earlier passing instance as arg. */
      admin.trigger("ready", instance);
    });

    // Add this line if Admin(config) is purely sequential.
    // instance.trigger("ready");
  }
});

/* Hope this will help a bit. But you should see it yourself in browser debugger to understand it clearly. */