0

I am loading up a framework (Microsoft AJAX Framework) along with Prototype and an autocomplete textbox. Both the framework and the autocomplete are adding behavior to some textboxes. I usually want both snippets to run, but not at all times. Basically, if an autocomplete item has been selected, I dont want the framework JS to run.

I thought I would try to overwrite the framework using a technique like this: Overriding a JavaScript function while referencing the original.

var original_doSomething = doSomething;
doSomething = function() 
{ 
    //do something *else* 
    if(something) original_doSomething();
}

However, the framework is a little clever and has been proving difficult to overwrite event handler is retaining the original function. I wrote a jsfiddle to mock the situation. http://jsfiddle.net/jeffrod/haHD4/2/

Is there anyway to overwrite dosomething so I can add my own logic to it??

Community
  • 1
  • 1
Jeff
  • 13,943
  • 11
  • 55
  • 103

2 Answers2

2

Can you just change this function yourself:

$(function()
{
    var someObject = { 'a':'aaa' }
    $('input').keypress(createHandler(someObject,dosomething));
});

Or reassign the keypress handler yourself. That function is binding dosomething to the event before you have a chance to change dosomething yourself.

Griffin
  • 13,184
  • 4
  • 29
  • 43
  • Unbind the original handler and then rebind mine? Is that what you mean? That could work. – Jeff Aug 10 '11 at 17:13
  • Awesome. that worked. I dont know why I hadnt thought of that. I guess I missed the forest for the trees. Thanks! – Jeff Aug 10 '11 at 17:54
0

You need to make sure your script runs after all the other library scripts have run. Here's an updated fiddle: http://jsfiddle.net/mrchief/haHD4/5/

Update: Due to the way jsfiddle renders, this demo may mislead. What I'm saying essentially is this:

This is how the script blocks should be registered (note the overwrite block is registered at very end, after all framework scripts):

<script src="MsAJax.js></script>
<script src="Prototype.js></script>
<script src="blahblah.js></script>

<script type="javascript">
    //this represents my file that i have control over. this 
    //js snippet runs after the framework

    window._dosomething = window.dosomething;
    function dosomething() {
        console.log('overwritten');
    }
</script>
Mrchief
  • 75,126
  • 20
  • 142
  • 189
  • this fiddle essentially put the new `dosomething` javascript function in the framework js file. i am unable to edit this file so this solution isnt going to work for me. thats why i put my `dosomething` function in a script tag in the markup. – Jeff Aug 10 '11 at 17:13
  • Oh no, when you put your stuff in markup, that always runs first (that's the way jsfiddle renders it). I put them at the very end to demo the idea I referred to. In your page, you don't have to modify the Framework js. Just make sure that the "overwrite" script block is written after all the framework script tags. Hope this makes sense. – Mrchief Aug 10 '11 at 17:20
  • @Mrcheif Ok, I see your point now. Unfortunately, I already tried this and it didnt work. It seems that because the `createHandler` function creates a new function, that new handler keeps a reference to the original method. Overwriting the method later on does not overwrite the handler's reference to the original method. – Jeff Aug 10 '11 at 17:43