1

I got a weird problem with my dojo app. It's simply about the scope and hitching the object itself. The following code

function(data) {
        console.info(this); // [I]
        var fun = require(["dojo/Deferred"], function(Deferred) {
            console.info(this); // [II]
        });

        lang.hitch(this, fun());
    }

creates this output

Object{...} // from [I]
Window index.php [II]

the desired output has to be 2 times the Object. I thought I understood the hitch-mechanism, but by invoking lang.hitch(this, fun()); it seems to me, that "this" is the object which is printed by [I].

I hope you guys can help!

Thanks in advance!

4

1 回答 1

3

您要做的是限制回调函数的范围:

function(data) {
        console.info(this); // [I]
        var fun = require(["dojo/Deferred"], lang.hitch(this,function(Deferred) {
            console.info(this); // [II]
        }));
}

而不是在被评估后挂钩到 require 函数的返回结果lang.hitch(this, fun());

于 2013-01-10T17:30:38.330 回答