0
votes

I have a js looks like:

    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 converted code looks like below

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

Got few questions

  1. how to call init method when i convert it into dojo amd?

  2. how to convert requestAnimFrame according to dojo?

  3. what is the correct approach as per AMD (methods inside return or var ={function abInit()} way?

1

1 Answers

1
votes

You can try to return a "Class" using declare and call your method in the constructor.

Example here:

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

Or return and object after, call your method after requiring your module, example:

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

  });