Issues aliasing the same DataManager function in two plugins

What is the correct method to use if I want to alias DataManager.onLoad in multiple plugins?


If I use this for both of them:


var _DataManager_onLoad = DataManager.onLoad;
DataManager.onLoad = function(object) {
_DataManager_onLoad(object);
// do my stuff here
};


the method in the second plugin calls the method in the first plugin, which calls the original method.  All three pass $dataActors as the object.  But I get an undefined is not a function error on rpg_managers 141: this.extractMetadata(data);


If I give them different alias names:


var _DataManager_onLoad1 = DataManager.onLoad;
DataManager.onLoad = function(object) {
_DataManager_onLoad1(object);
// do my stuff here
};


and


var _DataManager_onLoad2 = DataManager.onLoad;
DataManager.onLoad = function(object) {
_DataManager_onLoad2(object);
// do my stuff here
};


I get the same results.


On the this.extractMetadata(data) line it's telling me this is Window, but it's not listing extractMetadata as an available function.  When I disable both plugins and put a breakpoint on that line, it's telling me this is DataManager.  So I can see the context/scope is being shifted somewhere.


If I pass in this as a first argument:


var _DataManager_onLoad1 = DataManager.onLoad;
DataManager.onLoad = function(object) {
_DataManager_onLoad1(this, object);
// do my stuff here
};


and


var _DataManager_onLoad2 = DataManager.onLoad;
DataManager.onLoad = function(object) {
_DataManager_onLoad2(this, object);
// do my stuff here
};


the second plugin says this is DataManager and object is $dataActors, but when it gets through to the first plugin, it says this is Window and object is DataManager.  It then passes DataManager through to the original function instead of $dataActors, so nothing gets processed.


It doesn't seem to matter whether I use the same variable name for the aliases or not.


The second function is calling the first function, and the first is calling the original, but I can't seem to get the right results, whether I include this as an argument or leave it out.  What do I need to do so it passes through the correct arguments and knows that this is DataManager in each case?
You're not calling your aliases correctly. When you call an alias like that, you'd want to actually do so using the "call" function, because it can let you pick the scope that "this" takes. If you don't do so, I believe it copies the current scope form when its created, not 100% sure on that, but that's what would make sense here (as the alias is created in a global (and thus, window) scope). But anyway, the solution you're looking for is:

Code:
  var _DataManager_onLoad = DataManager.onLoad;
  DataManager.onLoad = function(object) {
    _DataManager_onLoad.call(this, object);
    // do my stuff here
  };
Brilliant!  Such a simple mistake.  Been a while since I've written one.


Thanks heaps 🙂
No problem, JavaScript can take a little getting used to, but once you understand how it works, you can make some pretty fancy stuff.
This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/issues-aliasing-the-same-datamanager-function-in-two-plugins.70882/