0

I have a class that gets created (testClass for this purpose). The class has the following method inside of it:

setExternalValue: function(val) {
        this.sliderCaption.innerHTML =  getPointsString(val*this.points_per_use, this.points_currency);
        this.sliderValuebox.value = this.getUses();
    }

How can I tie into this and listen for this method to happen? When it's completed, I would like to run a specific function. Putting a custom event inside of the setExternalValue will not work for me because I cannot edit the original js.

2
  • can't you just alert(setExternalValue(value)) ? It doesn't seem like setExternalValue is doing any async process. Commented Oct 22, 2015 at 18:12
  • Once this method happens I need to perform an ajax call to update some data within the page. Commented Oct 22, 2015 at 18:14

2 Answers 2

3

You can wrap it, call the original function, then insert your handler code.

var myObj = new testClass();   // or however you normally initialize them

myObj.oldSetExternalValue = myObj.setExternalValue;

myObj.setExternalValue = function(val) {
   var result = this.oldSetExternalValue(val);

   // whatever you wanted to run after it completes

   return result;
}

var testClass = function() {
  this.setExternalValue = function(val) {
    console.log("original setExternalVal " + val);
    return "return value";
  }
}

var myObj = new testClass();

myObj.oldSetExternalValue = myObj.setExternalValue;

myObj.setExternalValue = function(val) {
  var result = this.oldSetExternalValue(val);

  console.log("wrapper called with " + val + " after " + result);

  return result;
}

myObj.setExternalValue("foo");

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. I got this working. I knew it was something along these lines.
1

Make a reference to the old function. Replace the old function with something that calls the old function but also performs new stuff when it finishes.

var obj = {   
    setExternalValue: function(val) {
        alert("inside method: " + val);
    }
};
var referenceOldFunction = obj.setExternalValue;

obj.setExternalValue = function (val) {

    referenceOldFunction.call(obj, val);

    // do stuff after it ends
}

obj.setExternalValue("test");

1 Comment

Paul Roubs is better than mine.

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.