0

I have two js files. one is route.js and second one is xml.js. In route.js file I am calling a function which is in xml.js. What that function does is it parse through xml link and return xml object. When I do console.log($(xml)) inside xml.js then it shows xml but when I return this object and do console.log(Xml.init(param, param, param)) inside route.js then it returns undefined. WHY?

Here is my code

route.js

var Route = {
    fromurl : null,
    tourl   : null,
    from    : null,
    to      : null,

    init: function (fromaddress, toaddress) {
        from        = fromaddress;
        to          = toaddress;
        fromurl     = 'http://example.com/url=' + fromurl;
        tourl       = 'http://example.com/url=' + tourl;

        Route.searchRoute();
    },

    searchRoute: function () {
        console.log(Xml.init(from, to, fromurl)); // <---- this returns undefined why
    }

};

xml.js

var Xml = {
    to      : null,
    from    : null,
    url     : null,

    init: function (fromaddress, toaddress, link) {
        from    = fromaddress;
        to      = toaddress;
        url     = link;

        this.requestXml();
    },

    requestXml: function () {
        $.ajax({
            type: "GET",
            url: url,
            dataType: "xml",
            success: this.parseXml
        });
    },

    parseXml: function (xml) {
        console.log($(xml));  //<--- works absolutely fine here
        return $(xml);
    }
};

Update

I changed Xml object to following as well. Here I am converting xml to string and then string into json using [xmltojson][1] plugin but it still returns undefined. Please help me how can I resolve this issue?

var Xml = {
to  : null,
from    : null,
url     : null,

requestXml: function (fromaddress, toaddress, link) {
    from    = fromaddress;
    to      = toaddress;
    url     = link;

    $.ajax({
        type: "GET",
        url: url,
        dataType: "xml",
        success: function (xml) {
            var xmltostring     = Xml.xmlToString(xml),
            stringtojson    = Xml.stringxmlToJson(xmltostring);
            return stringtojson;
        }
    });
},

xmlToString: function (xmlData) { 
    var xmlString;
    if (window.ActiveXObject) {
        xmlString = xmlData.xml;
    } else {
        xmlString = (new XMLSerializer()).serializeToString(xmlData);
    }
    return xmlString;
},

stringxmlToJson: function (stringxml) {
    return $.xml2json(stringxml);
}
};
Om3ga
  • 30,465
  • 43
  • 141
  • 221

2 Answers2

4

Your code doesn't return the XML because of the AJAX-call (its asynchronous). Instead you can trigger an own event and listen to them.

This should work:

 var Route = {
    fromurl : null,
    tourl   : null,
    from    : null,
    to      : null,

    init: function (fromaddress, toaddress) {
        from        = fromaddress;
        to          = toaddress;
        fromurl     = 'http://example.com/url=' + fromurl;
        tourl       = 'http://example.com/url=' + tourl;

        Route.searchRoute();
    },

    searchRoute: function () {
        $(document).unbind('your_event').bind('your_event', function (event, xml) {
           console.log(xml);
        });
        Xml.init(from, to, fromurl);
    }
};

var Xml = {
    to      : null,
    from    : null,
    url     : null,

    init: function (fromaddress, toaddress, link) {
        from    = fromaddress;
        to      = toaddress;
        url     = link;

        this.requestXml();
    },

    requestXml: function () {
        $.ajax({
            type: "GET",
            url: url,
            dataType: "xml",
            success: this.parseXml
        });
    },

    parseXml: function (xml) {
        $(document).trigger('your_event', $(xml));
    }
};
  • 1
    Do I have to add Xml object and Route object in one file? – Om3ga Nov 08 '12 at 08:13
  • And what is `your_event` here? – Om3ga Nov 08 '12 at 08:15
  • You dont have to add Xml and Route code in one file. The event name "your_event" is just a sample. You can name it like you wish. The only thing is that the event name in your Route and Xml file must be the same. – Marco Francke Nov 09 '12 at 08:43
1

Your init-function does not return anything. Here's a possibility to make it work:

var Xml = {
    to      : null,
    from    : null,
    url     : null,
    result  : null,

    init: function (fromaddress, toaddress, link) {
        from    = fromaddress;
        to      = toaddress;
        url     = link;

        this.requestXml();
        return this;
    },

    requestXml: function () {
        $.ajax({
            type: "GET",
            url: url,
            dataType: "xml",
            success: this.parseXml
        });
    },

    parseXml: function (xml) {
            console.log($(xml));
        result = $(xml);
    },

    getResult: function () {
        return result;
    }
};

Then you can call:

searchRoute: function () {
    var xml = Xml.init(from, to, fromurl);
    console.log(xml.getResult());
}

Explanation

Your code tries to get the result from the ini()-method, which does not return the data. Instead, it calls the request-function, which calls parseXML() asynchronously - The result that parseXml() returns never ends anywhere. So, that's your code. What's the difference to mine?

In my code, init() does return an instance of the Xml-Class. With that given, parseXml can save the result in a variable (result here). The function getResult() then can give you the xml back.

looper
  • 1,929
  • 23
  • 42