Search code examples
javascriptcallbackintegration-testingsiesta

Passing parameters to callback function without changing method (Siesta Testing namespace)


Does anyone know how to pass parameters to a callback function that you cannot alter?

So here is the code I'm trying to implement (It is a siesta testing method):

Base.js:

waitForComponentQueryVisible: function (parentNext, parameters) {

    var th = this,
    query = parameters.query,
    scope = th,
    callback = callFunct,
    timeout = 10000;
//The function inside here (returnToParentTester(parentNext, parameters)) is from Base.js 
    callFunct = function () {
        scope.returnToParentTester(parentNext);
    }
    this.test.waitForComponentQueryVisible(query, callback, scope, timeout);
},

The problem here is of two parts: 1. I cant get the scope just right so I can use the returnToParentTester method that is found in Base.js 2. I want to pass in parentNext into the method but cannot do that when defining it as a callback

this is the method I need to run as the callback:

Base.js:

returnToParentTester: function (parentNext, parameters) {
    debugger;
    if (parentNext) {
        parentNext.call(undefined, parameters);
    } else {
        this.test.fail('--Developer Error-- undefined parentNext ' +
                       'variable. All the chains are going to fail');
    }
},

Solution

  • I can't get the scope just right so I can use the returnToParentTester method that is found in Base.js

    Use the call method to change the scope:

    Base.returnToParentTester.call(myScope)
    

    I want to pass in parentNext into the method but cannot do that when defining it as a callback

    Use an anonymous function as a callback and pass parameters within its scope:

    $("body").click(function(e){foo(e.target,bar,baz)})
    

    then let it do the work:

    function foo(myScope, next1, param1)
     {
     Base.returnToParentTester.call(myScope, next1, param1)
     }
    

    References