[Dojo-interest] Returning value from 1.7 require

Kitson Kelly kitson.kelly at asseverate.co.uk
Mon May 14 04:54:19 EDT 2012


You should wrap your whole section of code in a require...  You will have a
big issue with what you are doing because require is likely to
run asynchronous, which means that your return value will be undefined.

What is calling the function?  If it is going to be a globally scoped
function, then you could do something like this:

require(["dojo/query", "dojo/domReady!"], function(query){
  findRelevantElements = function(){
    return query(".relevant");
  }
  // ...
  findRelevantElements();
});


Or, you could do something like this:

require(["dojo/query", "dojo/domReady!"], function(){
  findRelevantElements = function(){
    var query = require("dojo/query");
    return query(".relevant");
  }

  // ...
  findRelevantElements():
});


But generally speaking, you need to start thinking of your code in an
asynchronous way with AMD.



On 14 May 2012 09:31, niels harremoes <nielsull at gmail.com> wrote:

> Hi!
>
> I am trying to return a value from a method, which uses some dojo
> libraries,
> whihc are loaded on demand using require:
>
> A simple example using dojo.query:
>
> function findRelevantElements()
>        var result = null;
>        require(['dojo/query'], function(query) {
>                result = query(".relevant");
>        });
>        return result;
> }
>
> This seems quite clunky, updating the result variable in the external
> closure from the internal function. Is there a simpler way?
>
>
> --
> View this message in context:
> http://dojo-toolkit.33424.n3.nabble.com/Returning-value-from-1-7-require-tp3985197.html
> Sent from the Dojo Toolkit mailing list archive at Nabble.com.
> ________________________________________________________
> Dojotoolkit: http://dojotoolkit.org
> Reference Guide: http://dojotoolkit.org/reference-guide
> API Documentation: http://dojotoolkit.org/api
> Tutorials: http://dojotoolkit.org/documentation
>
> Dojo-interest at mail.dojotoolkit.org
> http://mail.dojotoolkit.org/mailman/listinfo/dojo-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.dojotoolkit.org/pipermail/dojo-interest/attachments/20120514/8c8675ac/attachment.htm 


More information about the Dojo-interest mailing list