繁体   English   中英

dojo amd初始化方法自动运行

[英]dojo amd init method autorun

我有一个js看起来像:

    abinit();
    function abinit(){}
    function hello{var a=12; return a;}

    window.requestAnimFrame = (function(){
      return  window.requestAnimationFrame || 
              window.webkitRequestAnimationFrame || 
              window.mozRequestAnimationFrame || 
              window.oRequestAnimationFrame || 
              window.msRequestAnimationFrame || 
        function(/* function FrameRequestCallback // callback,// DOMElement Element  element){
        window.setTimeout(callback, 1000 / 60);
      };
     })();

AMD转换后的代码如下所示

 define(["dojo/ready","dojo/dom","dojo/dom-construct","dojo/_base /fx","dojo/fx","dojo/dom-style","dojo/parser","dojo/window", "dojo/dom-attr","dojo/domReady!"],  
     function(ready,dom,domConstruct,baseFx,coreFx,domStyle,parser,win,domAttr,) {
       var abGlobal = this;
       abGlobal.abStatus = false;
       return{
            abInit:function() { ...... },
            hellow:function(){var a=12; return a;}
      }
});

有几个问题

  1. 将其转换为dojo amd时如何调用init方法?

  2. 如何根据dojo转换requestAnimFrame?

  3. 按照AMD正确的方法是什么(return或var = {function abInit()}方法内部的方法?

您可以尝试使用declare返回“类”并在constructor调用您的方法。

这里的例子:

define([
        "dojo/ready",
        "dojo/dom",
        "dojo/dom-construct",
        "dojo/_base /fx",
        "dojo/fx",
        "dojo/dom-style",
        "dojo/parser",
        "dojo/window",
        "dojo/dom-attr",
        "dojo/_base/declare",
        "dojo/domReady!"
      ],
      function(ready, dom, domConstruct, baseFx, coreFx, domStyle, parser, win, domAttr, ) {
        var abGlobal = this;
        abGlobal.abStatus = false;
        return declare(null,{
          constructor:function(){
            this.abInit(); // call your init here
          },
          abInit: function() {
          },
          hellow: function() {
            var a = 12;
            return a;
          }
        });
      });

或返回和对象之后,在需要模块后调用您的方法,例如:

define([
    "dojo/ready",
    "dojo/dom",
    "dojo/dom-construct",
    "dojo/_base /fx",
    "dojo/fx",
    "dojo/dom-style",
    "dojo/parser",
    "dojo/window",
    "dojo/dom-attr",
    "dojo/_base/declare",
    "dojo/domReady!"
  ],
  function(ready, dom, domConstruct, baseFx, coreFx, domStyle, parser, win, domAttr, ) {
    var abGlobal = this;
    abGlobal.abStatus = false;
    // return an object here
    return { 
      abInit: function() {
      },
      hellow: function() {
        var a = 12;
        return a;
      }
    };
  });

  require(['yourModule'],function(yourModule){
    yourModule.abInit(); // call your method here

  });

暂无
暂无

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

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