为什么这个(javascript)关闭失败?

时间:2012-09-01 15:40:54

标签: closures qunit

当应该设置为true时,变量“called”为false ..为什么会这样? 当插件调用时它被设置为true但在闭包之外它仍然是假的。 它有点莫名其妙。提前感谢任何指示。

(function() {
    module("when InitializedApplication() is called");
    test("it should call the success function", function () {
    // arrange
    $("#qunit-fixture").append(
        '<script id="events-catalog-view-template"' +
        '        type="text/html"'+
        '        src="_events-catalog.view.html">' +
        '</script>' +
        '<div id="events-catalog-view-container"' +
        '     data-bind="template: {' +
        '             name="events-catalog-view-template" ' +
        '             afterRender="tpw.mediator.eventscatalog.setupViewDataBinding" ' +
        '          }"' + 
        '</div>'
    );              

    var called = false;

    // act
    var init = TPW.InitializeApplication();

    init({
        logLevel: "debug",
        success: function (successfullResolution) {                               
            called = true;
        },
        error: function (failedResolution) {                            
        }
    });

    // assert                
    ok(called, "success function called");
    });    
})();

1 个答案:

答案 0 :(得分:0)

在成功回调之前调用了qunit ok()函数。异步问题。