Reputation: 53
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?
Upvotes: 3
Views: 48
Reputation: 2047
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();
Upvotes: 2