1

Simple question.

Does AMD DOJO implementation support these type of declarations?

  1. text!./plain.html

    define(["../Widget","text!./plain.html"],
      function(Widget,plain){
      return new Widget({name:"mainApp",template:plain});
    }); 
    
  2. Load non-modules, let's say underscore.js

    require(['dir/underscore.js'], function(){
     _.reduce ...
    });
    
GSerg
  • 76,472
  • 17
  • 159
  • 346
Alex Pi
  • 816
  • 4
  • 12
  • 28

1 Answers1

2

Yes, but the precise syntax is different to that used in the question.

The plugin for loading character data is dojo/text.

The extension should not be used when loading a JavaScript library and the location of the file is set via either a relative of the dojotoolkit base or a packages location declaration in dojoConfig:

require(['underscore'], function( _ ){
 _.reduce ...
});

Configure the namespace in the Dojo configuration to avoid messy import paths - see dojoConfig in the loader documentation.

Also, consider using the dojo/global module and/or defining a Dojo module as a wrapper for Underscore.js:

//_.js
define(['dojo/global', 'underscore'], function(global){
  return global._
});

With the above consideration, you must have loaded the actual .js file manually. If in conjunction with the dojo/text plugin, one would create a wrapper which also loads the required JS and evaluates it, following could do the trick.

/var/www/dojo-release-1.7/ext_lib/_.js - this sample file is hierachially placed in a library namespace, alongside dojo, dijit, dojox

define(['dojo/global', 'dojo/text!./Underscore.js'], function(global, scriptContents){
  global.eval(scriptContents);
  return global._ // '_' is the evaluated reference from window['_']
  /** 
   * Alternatively, wrap even further to maintain a closure scope thus hiding _ from global
   *  - so adapt global.eval to simply eval
   *  - remove above return statement
   *  - return a dojo declared module with a getInstance simple method
  */
  function get_ () { return _ };
  var __Underscore = declare('ext_lib._', [/*mixins - none*/], {
        getInstance: get_
  });
  // practical 'static' reference too, callable by 'ext_lib.getInstance' without creating a 'new ext_lib._'
  __Underscore.getInstance = get_;
  return __Underscore;
});

A sample of defining own modules using declare here Note: this code is untested; feel free to add corrections.

Community
  • 1
  • 1
McDowell
  • 107,573
  • 31
  • 204
  • 267
  • 1
    note about the first require in this text; by only telling toplevel namespace to require - a proper declaration of package 'underscore' must be supplied, following 'Dojo’s package concept', meaning 'underscore' is a directory and it contains a package descriptor (see CommonJS) – mschr Jul 15 '12 at 23:24
  • It works for me (using your first approach). I tried using the AMD supported underscorejs fork as described here: http://stackoverflow.com/questions/8131265/loading-backbone-and-underscore-using-requirejs. It did not work right away for me so I settled for this approach. Was anyone able to load this AMD underscore fork with the Dojo AMD loader? – Frederic Fortier Jan 25 '13 at 00:05