3

In the wapp.js I have the following class in JavaScript:

function Wapp() {
    this.page = function($page_name) {
        this.onLoad = function($response) {

        }
    }
    this.navigate = {
        changePage: function(link) {
            // Ajax request to load the page
            $.post(link, {}, function(response) {
                // Code to display the page
            });
        }
    }
}

in the app.js script I have the following code:

var wapp = new Wapp();

and I want to do this:

wapp.page('home').onLoad(function() {
    // doSomething();
}

// When I call the method 'changePage'
wapp.navigate.changePage('home');

// at the end of the page load 'home' I want to call the function 'doSomething()' inside the 'onLoad' method

how should I define the methods within the class to make sure that at the end of a certain action (in this case at the end of the ajax call) to run the code defined within the 'onLoad' method in app.js?

1 Answer 1

2

You are able to assign a function as variable to your class when constructing it. You'll be able to call assigned function later in your code (e.g. in end of the other function)

https://jsfiddle.net/L8c2s6xr/

function func (functionToCall) {
  this.functionToCall = functionToCall;

  this.doSomething = function () {
    this.functionToCall();
  }
}

var f = new func (
  function() {
    alert('this works');
  }
);

f.doSomething();
Sign up to request clarification or add additional context in comments.

Comments

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.