0

I was trying to call the callback function with classname, defined in another javascript file, but I was failed to call. I didn't get the mistake I did. Please let me know the mistake I did in below code and Thank you show much for your help.

I have created one central javascript file like below

CentralScript.js

function CentralScript() {

}

CentralScript.prototype.makeRequest = function (className, cbf, dataToSend) {
    $.ajax({
        url: 'apiurl',
        type: 'POST',
        dataType: 'json',
        data: {
            parameters : dataToSend
        },
        success: function (responseData) {
            this.showResponse(className, cbf, responseData);
        },
        complete: function() {

        },
        error: function () {
            console.log("Error occurred...");
        }
    });
};

CentralScript.prototype.showResponse = function (className, cbf, data) {
    className.cbf(data);
};

and I have created another file like below

SomeFile.js

function SomeFile() {

}

SomeFile.prototype.sayHi = function() {
    var obj = new CentralScript();
    var dataToSend = {
        method: 'someMethod'
    };
    obj.makeRequest('SomeFile', 'resultToShow', dataToSend);
};

SomeFile.resultToShow = function (data) {
    console.log(data);
};

and I have created main.js file like below

main.js

var ProjectName= (function() {

    var sfObj;

    function init() {
        createObjects();
        initiateProject();
    }

    function createObjects() {
        sfObj = new SomeFile();
    }

    function initiateProject() {
        sfObj.sayHi();
    }

    return {
        init : init
    };

})();
$(ProjectName.init);

I was getting the response when I was making ajax request from SomeFile.js file, but the response was not logging in console.

I am getting 'cbf' as undefined in 'showResponse' function present in CentralScript.js file

CentralScript.prototype.showResponse = function (className, cbf, data) {
   className.cbf(data);
};

May I call the callback function like this "className.cbf(data);" present in SomeFile.js

Please let me know the mistake I did and Thank you show much for your help.

Sivakumar Tadisetti
  • 1,007
  • 7
  • 12
  • 2
    `className` is a **string** and strings don't have a property with name `cbf`. – Felix Kling Apr 23 '16 at 15:02
  • `this` at `this.showResponse(className, cbf, responseData);` would reference request object, not `CentralScript` – guest271314 Apr 23 '16 at 15:35
  • Have a look at [dot notation vs bracket notation](https://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets). You should not pass names around - just pass the objects they stand for! – Bergi Apr 23 '16 at 16:06

1 Answers1

0

The problem has nothing to several files. This is corrected script:

//CentralScript.js
    function CentralScript() {

    }

    CentralScript.prototype.makeRequest = function (className, cbf, dataToSend) {
        var $this = this;//save for further use
        $.ajax({
            url: 'apiurl',
            type: 'POST',
            dataType: 'json',
            data: {
                parameters: dataToSend
            },
            success: function (responseData) {
                //cbf(responseData);//cbf is SomeFile.resultToShow
                $this.showResponse(className, cbf, responseData);//this is $.ajax here
            },
            complete: function () {

            },
            error: function () {
                console.log("Error occurred...");
            }
        });
    };

    CentralScript.prototype.showResponse = function (className, cbf, data) {
        //className.cbf(data);//undefined
        cbf(data);//cbf is SomeFile.resultToShow
    };

//SomeFile.js
        function SomeFile() {

    }

    SomeFile.prototype.sayHi = function () {
        var obj = new CentralScript();
        var dataToSend = {
            method: 'someMethod'
        };
        //obj.makeRequest('SomeFile', 'resultToShow', dataToSend);
        obj.makeRequest(this, this.resultToShow, dataToSend);//this is SomeFile
    };

    SomeFile.prototype.resultToShow = function (data) {//need .prototype to add function to SomeFile
        console.log(JSON.stringify(data));
    };
Alex Kudryashev
  • 9,120
  • 3
  • 27
  • 36