javascript - .js file with just a require([" "," ",..., function(){}]) inside -


my question use of "require" statement javascript. i'm starting working js , dojo , i've following issue:

i'm developing plug-in web site , main java class of plugin make reference through main web application api. .js

public string getscript() {     return "samplesearchchoicelistplugin.js"; } 

this samplesearchchoicelistplugin.js has require, array of paths , function overrides 2 functions inside body. (the code of functions not listed because think irrelevant). has following content:

require(["dojo/_base/declare",          "dojo/_base/lang",          "ecm/widget/singlepropertyeditorfactory",          "samplesearchchoicelistplugindojo/searchchoicepane"          ],function(declare, lang, singlepropertyeditorfactory, searchchoicepane) {          /* use function add global javascript methods plug-in requires.*/     singlepropertyeditorfactory.prototype.createsinglepropertyeditor = function(kwargs) {          },     singlepropertyeditorfactory.prototype._createsearchchoicelisteditor = function(baseconstraints, kwargs) {      }; }); 

samplesearchchoicelistplugin.js being loaded in target web application because firebug recognizing script. set breakpoints on the proimagelayoutplugin.js checked javascript not being run or triggered of actions supposed trigger this.

i'm new development js , great if explain me how these "require" scripts work or lead me link in explained? read related requirejs not sure... thanks

in dojo, require amd loader. can think of way of importing modules, import statement in java , python (et al.).

  • the first argument array of strings, paths modules want load.
  • the second argument function callback, names each of modules specified in module array. in general, if first array has n elements, function should take n arguments.

inside function callback, can use modules. keep in mind scoped, outside function, these modules not accessible.

here's example of code run require statement, creates button alerts "hello, world!" when clicked:

require([     'dijit/form/button',      'dojo/on',      'dojo/dom',      'dojo/dom-construct' ], function(button, on, dom, domconstruct) {     var btn = new button({ label: 'hello!' });     on(btn, 'click', function(e) {         alert('hello, world!');     });     domconstruct.place(dom.byid('somenodeid'), btn.domnode); }); 

if want plugin run, need turn module. fortunately, pretty simple define, similar require, except used create module (think, "class") instead of define necessary components block of code. here's example dojotoolkit reference guide:

define([     "dojo/_base/declare",     "dojo/dom",     "app/dateformatter" ], function(declare, dom, dateformatter) {     return declare(null, {         showdate: function(id, date){             dom.byid(id).innerhtml = dateformatter.format(date);         }     }); }); 

this defines module has public showdate function. can structure plugin , call in module load into.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -