0

I need to call "MyOtherFunction" when "MyFunction"(which creates an element) completes, without MyFunction knowing what MyOtherFunction is.

The reason I need this is for extension of a jquery powered fileupload User Control that is used in several places with different functionality. A specific page shows a header and file count for it, and when the upload completes, I need to modify the file count according to how many files are displayed(by created elements) I thought :

$(UserControl).on(MyFunction, UploadElem, MyOtherFunction);

but this route is not accomplishing anything. The most I can alter the User Control is add in a function call, but without effecting the original user control functionality.

I'm not sure if because MyFunction isn't an event and doesn't bubble up or if it just isn't possible to use a defined function as a parameter of .on() is the reason I cannot get this code to work. Any suggestions?

2 Answers 2

2

Easiest way I can think of, is duck punching respectively hooking that method:

var _oldMyFunction = MyFunction;

MyFunction = function() {
    _oldMyFunction.apply( this, arguments );
    MyOtherFunction();
};
Sign up to request clarification or add additional context in comments.

3 Comments

That may work. I'm not sure about in my circumstance, but this answer definitely looks like it will check out and AT LEAST help others, thanks!
Unfortunately for my situation, this route doesn't accomplsh what I need it to standing alone. I may add in a ClientUploadFinished function without need for event parameters and see if diong so I may be able to use this functionality to accomplish what I need using a generic extension method
I did use the concept in your answer to solve my issue, so I will mark as my answer, but please, look at my answer to see how exactly I used it!
0

I managed to solve my own issue, but the context is important for the answer:

// Using a Global JavaScript object I created:
GlobalNameSpace.ExtensionFunction = function(oParam1, oParam2, oParam3)
{
   /// <summary>All parameters are optional</summary>

   return; // For instances when it is not being overwritten, simply return
}

//In the Code for the user control:
GlobalNameSpace.UploadControl.UploadComplete(oSender, oArgs)
{
    ///<summary>Handles the Upload process</summary>

    // process the upload
    GlobalNameSpace.ExtensionFunction(oSender, oArgs);
}

//and finally in the code to extend the functionality
GlobalNameSpace.Page.Init
{
    ///<summary>Initializes the page</summary>

    // redefine the extension function
    GlobalNameSpace.ExtensionFunction = function(oSender, oArgs)
    {
      GlobalNameSpace.Page.Function(oSender, oArgs);
    }
}

This allows me to extend anything I need it to without polluting my objects, and having something generic already existing to call on to make my changes. This solution solves my problem of needing a onCreate function for the elements I create to represent my uploaded items to trigger the header displaying the number of files. Very useful

2 Comments

Overall this question was a situation occurrence that ended getting solved by making my object architecture a bit more complex. The answer here is just a bandaid
This format is not an optimal answer as any object can overwrite it in the global namespace I have made. I'll have to revisit this..

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.