1

I am working with jQuery. I have a scenario where I have one callback method/function that is being called by two different functions/methods.

Problem: how can I know in my callback method/function that which of the two functions / methods called my callback method / function? Because I have some variables that I want assign some values depending upon which method/function called the callback.

Sample code:

var One = "";
var Two = "";

function first(){
    var urlink = "https://192.168.150.3/api1?jsonpCallback=myCallback";
    $.ajax({
        type: "GET",
        url:  urlink,
        dataType:"jsonp",
        async: false,
    });
}

function second(){
    var urlink = "https://192.168.150.3/api2?jsonpCallback=myCallback";
    $.ajax({
        type: "GET",
        url:  urlink,
        dataType:"jsonp",
        async: false,
    });
}

myCallback = function(data){
        /* if the caller is first then
        alert('first') & set value of var One; 
        else alert("second") & set value of var Two;
        How to do this?*/
        }

As far as I have read here and there, there is no solution and it didn't work for me atleast. Any suggestions/work around will be appreciated. :)

Community
  • 1
  • 1
ρss
  • 5,115
  • 8
  • 43
  • 73
  • 6
    I'm sorry, why duplicate a function? Simply call the same function but pass an extra param and use this extra param! – MrPk Oct 13 '14 at 14:46
  • Avoid the use of `async:false`.... http://stackoverflow.com/questions/6902174/tactics-to-avoid-using-asyncfalse-with-jquery – Hackerman Oct 13 '14 at 14:59
  • MrPk Thanks. that is also a good solution. But can you please confirm that there is no other solution for this problem. As described in the links I provided in my quesion. – ρss Oct 13 '14 at 15:06

2 Answers2

1

Try

$(function () {
    var one = "",
        two = "",
        _name = "",
        _url =  urlink; // without `?jsonpCallback=callback"`
    callback = function (data) {        
            $("#result")
            .append("<br>" + _name + ":" + data.result);
            alert(_name)
    };
    var cb = function (name) {
        $.ajax({
            beforeSend: function (jqxhr, settings) {
                _name = name
                if (_name === "first") {
                    one = _name
                } else {
                    two = _name
                }
            },
            type: "GET",
            url: _url,
            dataType: "jsonp",
            jsonpCallback: "callback"
        })
    };
    // call as `cb("first")` , `cb("second")
    // allow 1s between calls
    $.when(cb("first"), 
           setTimeout(function() {
               cb("second")
           },1000))
});

See also

Pass additional parameter to a JSONP callback ,

Can I make a jQuery JSONP request without adding the '?callback=' parameter in URL?

$(function () {
    var one = "",
        two = "",
        _name = "",
        _url =  "https://gist.githubusercontent.com/"
                + "anonymous/9a6997f09de9b68c59b2/"
                + "raw/f7d7b756005ad6d2b88cf0211f78a2990d7d2dc7/"
                + "content.json";
    callback = function (data) {        
            $("#result")
            .append("<br>" + _name + ":" + data.result);
            alert(_name)
    };
    var cb = function (name) {
        $.ajax({
            beforeSend: function (jqxhr, settings) {
                _name = name
                if (_name === "first") {
                    one = _name
                } else {
                    two = _name
                }
            },
            type: "GET",
            url: _url,
            dataType: "jsonp",
            jsonpCallback: "callback"
        })
    };
    $.when(cb("first"), 
           setTimeout(function() {
               cb("second")
           },1000))
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div id="result"></div>
Community
  • 1
  • 1
guest271314
  • 1
  • 15
  • 104
  • 177
  • So you are also doind what MrPk suggested, to pass additional parameters to the callback method to distinguish between the caller methods. – ρss Oct 14 '14 at 12:23
  • @ρss Yes. Could alternatively append the (`fn`) `name` to the `url` string to distinguish calls to `cb` , then parse `url` string for `name` . – guest271314 Oct 14 '14 at 15:06
0

I have updated code. see if this helps you and inform. fiddle

See does this helps you.

var tryFun = function(obj) {
        var callerFunction =this.name;
        alert(obj +" calling function "+callerFunction);

    };

function first() {
    this.name="first";
        tryFun.call(this,"akash");
    }

function second() {
    this.name="second";
        tryFun.call(this,"akash");
    }

first();
second();
Akash Sarawagi
  • 207
  • 1
  • 5