0

I'm trying to call a function as a callback from another function within the same file.

Code below, shows no errors, but does nothing - is there a different/better way to do this?

I'm calling first function from require config file (require-main.js) and that works fine

define(['require-main'], function() {
    require(['getJSON'], function(getJSON) {
        getJSON.stations();
    });
});

getJSON.js

define(['jquery', 'domChange', 'setUp'], function($, domChange, setUp) {
    var self = this;
    return {

        stations: function() {    
            $.get('/js/ajax/stations.json', function(sol) { /* local JSON */
                tmv.stations = sol;
                console.log(sol); /* this is fine */
                self.lines; /* <-- call next function */
            });
        },

        lines: function() {
            console.log('foo'); /* NOT called */
            $.get('/js/ajax/lines.json', function(lines) { /* local JSON */
                /* do something */
            });
        }
    }
});

I've seen this question but I can't work this way as the order is not predetermined

Update: as above, tried caching this into var but still no joy

1
  • this.lines this is the reference of the function, you are not actually calling it Commented Aug 4, 2016 at 7:45

3 Answers 3

3

Try this for the contents of your getJSON.js:

define(['jquery', 'domChange', 'setUp'], function($, domChange, setUp) {
    var getJSON = {

        stations: function() {    
            $.get('/js/ajax/stations.json', function(sol) { /* local JSON */
                tmv.stations = sol;
                console.log(sol); /* this is fine */
                getJSON.lines(); /* <-- call next function */
            });
        },

        lines: function() {
            console.log('foo'); /* NOT called */
            $.get('/js/ajax/lines.json', function(lines) { /* local JSON */
                /* do something */
            });
        }
    }
    return getJSON;
});
Sign up to request clarification or add additional context in comments.

Comments

1

Should be

tmv.stations = sol; console.log(sol); /* this is fine */ this.lines(); /* <-- Actually calls the function */

Comments

0

Maybe you should keep the reference to this keyword before calling the ajax function

stations: function() {    
        var self = this;
        $.get('/js/ajax/stations.json', function(sol) { /* local JSON */
            tmv.stations = sol;
            console.log(sol); /* this is fine */
            self.lines(); /* <-- call next function */
        });
 }

2 Comments

he already does, and anyway this refer to the object in that case, not to the function
@Francescoes. BTW his var self = this is not at the same placed, I think my solution should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.