【问题标题】:dojo amd init method autorundojo amd init 方法自动运行
【发布时间】:2016-04-28 15:02:05
【问题描述】:

我有一个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()} 方式?

【问题讨论】:

    标签: javascript dojo initialization


    【解决方案1】:

    您可以尝试使用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;
              }
            });
          });
    

    或者return and object after,在需要你的模块之后调用你的方法,例如:

    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
    
      });
    

    【讨论】:

    • 谢谢。使用第二个解决方案,它的工作。如何根据dojo转换requestAnimFrame
    • @angel 我认为dojo中没有类似requestAnimFrame的功能,所以你可以简单地在你的应用程序中实现它作为vanilla js。
    • 如果您发现我的回答有用,请不要忘记接受回答 :) 点击左侧的绿色标记。谢谢
    • init 现在工作正常,但是在进行 xhr 调用时,小部件注册表值未定义。我需要在 xhr 响应后再次进行 init 调用吗?
    • @angel 请考虑使用最小可行代码创建一个 jsfiddle 以重现您的错误。请将其作为一个新问题发布,我们将非常乐意为您提供帮助:)
    猜你喜欢
    • 2013-06-10
    • 2012-10-10
    • 1970-01-01
    • 1970-01-01
    • 2012-02-29
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多