javascript - Trouble with Dojo Build and Conversion to AMD -


i'm working on dojo application written else. i'm new dojo, have discovered reduce http requests need produce "build" version. have done, afaict (i single compressed script @ least), when using built script, none of functions available work (they're "undefined").

furthermore, in trying figure out, looks though point make code amd compatible (is code amd compatible?). how work examples such 1 below? reading appears might need make each existing function scripts module, produce hundreds of scripts, , doesn't feel right. how best convert code such amd compatible , buildable?

i've got 15 or .js scripts containing varying numbers of functions written way...

tia!

var somethingstatus = false;  somethinginit();  function somethinginit() {      require(["dojo/ready", "dojo/dom", "dojo/dom-construct", "dojo/cookie", "dojo/json", "dojo/domready!"], function(ready, dom, domconstruct) {          ready(function() {              var content = '';             // content generated here, then...              domconstruct.place(content, dom.byid('body'));          });      });  }  function somethingtotop(target) {      require(["dojo/dom", "dojo/dom-style", "dojo/_base/fx", "dojo/window", "dojo/domready!"], function(dom, domstyle, fx, win) {          var vs = win.getbox();          somethingbarstatus = true;          fx.animateproperty({             node: dom.byid('somethingbar'),             properties: {                 top: { start: domstyle.get('somethingbar', 'top'), end: 0 },                 height: { start: domstyle.get('somethingbar', 'height') + (domstyle.get("somethingbar", "padding") * 2), end: vs.h }             },             duration: 500,             onend: function() {                 document.location = 'http://192.168.0.1' + target;             }         }).play();      });  }  function somethingemptytop() {      require(["dojo/dom", "dojo/dom-construct", "dojo/domready!"], function(dom, domconstruct) {           globalcontainerempty('somethingtop'); // function in .js file, constructed      });  }  // many more functions below , across other scripts! 

you're running common problem when migrating pre-amd dojo dojo 1.7, in many people attempt run scripts through build entirely not modules. dojo build tool designed accommodate modules, not loose scripts, sort of thing happened "just work" before.

in example above, have script seems define number of global functions. if anything, it's inverse of proper module, since each individual function involves own require call.

when dojo build encounters file detects isn't in amd format, puts amd wrapper around it, purpose of adapting proper legacy dojo modules use dojo.provide, dojo.require, , global namespaces dojo , dijit. problem is, when these "global" functions in script wrapped, become local define factory in wrapper, , no longer global.

a proper conversion of code above this:

define([     'dojo/_base/fx',     'dojo/cookie',     'dojo/dom',     'dojo/dom-construct',     'dojo/dom-style',     'dojo/json',     'dojo/window',     'my/othermodule',     'dojo/domready!' ], function (fx, cookie, dom, domconstruct, domstyle, json, win, othermodule) {      var somethingstatus = false;      var util = {         somethinginit: function () {             var content = '';             // content generated here, then...              domconstruct.place(content, dom.byid('body'));         }          somethingtotop: function (target) {             var vs = win.getbox();              somethingbarstatus = true;              fx.animateproperty({                 node: dom.byid('somethingbar'),                 properties: {                     top: { start: domstyle.get('somethingbar', 'top'), end: 0 },                     height: { start: domstyle.get('somethingbar', 'height') + (domstyle.get("somethingbar", "padding") * 2), end: vs.h }                 },                 duration: 500,                 onend: function() {                     document.location = 'http://192.168.0.1' + target;                 }             }).play();         },          somethingemptytop: function () {             // assuming other module converted 1             othermodule.globalcontainerempty('somethingtop');         }     };      util.somethinginit();     return util; } 
  • notice of dependencies formerly in each individual function have been collected in define call module.
  • dojo/ready not needed amd since dojo/domready! waits domcontentloaded (or equivalent), , define factory waits until modules loaded.

you able access each of functions on module loading via define in module or require in page script, referencing functions via variable store module in. (in example, othermodule example of using converted module, since suggested globalcontainerempty in similar script file.)

the modules tutorial can provide further help, , possibly modern dojo tutorial.


Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -