I would like to extend $.mobile.changePage to accept more options such as adding a callback function for when the page finishes loading as well as more options for the AJAX call like contentType. Is there a way to do this without changing the source code? If not, I am willing to change the source code for educational purposes, but could not find it in the jQuery Mobile GitHub: https://github.com/jquery/jquery-mobile . Thanks for any helps or guidance.
I would like to extend $.mobile.changePage to accept more options such as adding a
Share
One of the more exciting parts of JavaScript is that ability to redefine any function using a technique which is commonly referred to as Monkey Patching. (as an aside ES5 provides a new freeze method which allows developers to prevent such modifications.)
Here’s an example of a JavaScript MonkeyPatch which allows us to modify the behaviour of a function without editing it’s source:
Say we wanted to add logging to the sum method, we could just add a
console.logline to the function, but we can also monkey patch it:Now when
Example.sumis called, not only will we get the result as before, but a console message will also be written. With this in mind, you can monkey patch the$.mobile.changePagemethod in the same way: