PDA

View Full Version : [4.0.1] Create function interceptor



ap
7 Jun 2011, 10:15 AM
Please forgive me if this does not end up being a bug. I have reached my ability/time limit of investigating this any further.

The Ext.Function.createInterceptor function when used to intercept a static function (in my cast, Ext.util.History.add) does not seem to call the original function with the correct object.

I have made a simple example to demonstrate that.

The page will call the Ext.util.History.add function twice, then an interceptor is created, and then function is called. The block in the interceptor function will be executed correctly, but the when the original History.add function is called, it fails.

Stepping through the add function, you will see that after the interceptor is called, the Window object is used to call the original add function.

I'm not sure if this is a bug of the createInterceptor function or the History.

Self contained example of the problem.


<html>
<head>
<title>ExtJs 4 - function intercept test</title>
<!-- Ext includes -->
<link rel="stylesheet" type="text/css" href="http://dev.sencha.com/deploy/ext-4.0.0/resources/css/ext-all.css" />
<script type="text/javascript" src="http://dev.sencha.com/deploy/ext-4.0.0/ext-all-debug.js"></script>

<script type="text/javascript">

Ext.onReady(function() {

Ext.util.History.init();

Ext.util.History.add('atoken', true);

alert("history added, see URL, a new history token will be added after this dialog is dismissed");

Ext.util.History.add('anothertoken', true);

var myInterceptor = Ext.Function.createInterceptor(Ext.util.History.add, function( tk , nodup){
if(console){console.info('interceptor function called with token: \''+tk+'\', calling original function History.add() (will fail)');}
return true;
});

myInterceptor('newtoken', true);

});
</script>
</head>
<body>
<form id="history-form" class="x-hide-display">
<input type="hidden" id="x-history-field" />
<iframe id="x-history-frame"></iframe>
</form>
</body>
</html>

https://lh3.googleusercontent.com/-t7poLcGe4og/Te5qS4TlxDI/AAAAAAAAFuU/9hz_JQ6q5Eg/s800/Screen%252520shot%2525202011-06-07%252520at%2525201.52.45%252520PM.png
https://lh4.googleusercontent.com/-FqgBeTqKhn8/Te5qS-waYnI/AAAAAAAAFuQ/vsdypERcDOg/s800/Screen%252520shot%2525202011-06-07%252520at%2525201.53.48%252520PM.png

ap
7 Jun 2011, 12:02 PM
Okay, the following change to the above code works.


//var myInterceptor = Ext.Function.createInterceptor(Ext.util.History.add, function( tk , nodup){
Ext.util.History.add = Ext.Function.createInterceptor(Ext.util.History.add, function( tk , nodup){
if(console){console.info('interceptor function called with token: \''+tk+'\', calling original function History.add()');}
return true;
}, Ext.util.History);

Ext.util.History.add('newtoken', true);
//myInterceptor('newtoken', true);


The documentation says,
If omitted, defaults to the scope in which the original function is called or the browser window.

BUT! using another function such as 'myInterceptor= Ext.Function.createInterceptor ....' does not work even with the scope specified.


var myInterceptor = Ext.Function.createInterceptor(Ext.util.History.add, function( tk , nodup){
if(console){console.info('interceptor function called with token: \''+tk+'\', calling original function History.add()');}
return true;
}, Ext.util.History);
myInterceptor('newtoken', true);