We've developed a sort of mobile application kit which was initially designed to run without any user code. Its part of a mobile app studio and now we'd like to enable "user overrides" through Javascript.
Right now we're experimenting with 3 ideas :
- the event pattern
- the hook pattern
- "monkey patching" (prototype overwrite of existing functions)
All ways have its pros and cons. For instance,
the event pattern currently implemented with dojo's subscribe and publish
the pros are nice : clean and minimal additional code in the core code. the problem : the code execution already took place and the user only gets notifications what happened in the core. In order to apply his customization, he needs reverse the changes and rolls out his on own code. Thats somehow messy and not reliable as the core could rely on something later which doesn't exist anymore.
the hook pattern. Lets say its implemented similar to "delegates"
ie, the core code does determine by default whether a peace of content is sharable :
xapp.utils.getModuleViewCreationParameters = function (content, viewManager, selected,type,heading)
{
var isShareable = false;
if(type!=null)
{
isShareable=xapp.utils.isSharable(type);
}
To enable the user to override this behavior but leave the default intact, I would need to extend it like this :
xapp.utils.getModuleViewCreationParameters = function (content, viewManager, selected,type,heading)
{
var isShareable = false;
if(type!=null)
{
isShareable=xapp.utils.isSharable(type);
}
if(ctx && ctx.getShareDelegate && ctx.getShareDelegate() && ctx.getShareDelegate().isSharable){
isShareable=ctx.getShareDelegate().isSharable(type);
}
Now, "ctx.getShareDelegate()" returns the user's actual class which can be used override the default behaviour.
The pros : its still clean and modifies the core's flow more direct. The cons : it could lead to a sort of spaghetti code but I am not sure about this.
And my last idea : prototype modification aka monkey patching
The pros : it certainly allows any core override but : the cons : the user code relies on too many assumptions such as the core code's function signatures remain the same.
Well, after all my favorite is still "the hook or delegate pattern".
Can you recommend any alternatives or suggestions to this problem ? Or to design a user customizable JS API else ?