1. #1
    Sencha User steffenk's Avatar
    Join Date
    Jul 2007
    Location
    Haan, Germany
    Posts
    2,649
    Vote Rating
    6
    steffenk has a spectacular aura about steffenk has a spectacular aura about steffenk has a spectacular aura about

      0  

    Default How to remove an Interceptor or a sequence

    How to remove an Interceptor or a sequence


    Hi,

    there is functionality to createInterceptor or createSequence which allows great hooks, but how can i remove them? Is there a way to access the chain, i miss that in the api.

    I found removeMethodListener, but comment mark it as experimental. is there any example available?
    vg Steffen
    --------------------------------------
    Release Manager of TYPO3 4.5

  2. #2
    Sencha User steffenk's Avatar
    Join Date
    Jul 2007
    Location
    Haan, Germany
    Posts
    2,649
    Vote Rating
    6
    steffenk has a spectacular aura about steffenk has a spectacular aura about steffenk has a spectacular aura about

      0  

    Default


    found something in deep of this forum from Jack:
    myObj.removeMethodListener('myMethod', Base.process, Base);

    i will build a testcase for that to check it out.
    Again: why are these functions marked with experimental? Noone use it?
    vg Steffen
    --------------------------------------
    Release Manager of TYPO3 4.5

  3. #3
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,483
    Vote Rating
    35
    Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    I assume we are talking about member functions of Ext classes?

    If you have poked the interceptor into a prototype property, then the only reference to the original, anonymous function is within the interceptor, and you can never get it back. You have replaced the prototype's "pointer", and now have it pointing towards your interceptor function.

    if you think you might need to restore, then you must store a reference.

    Perhaps createInterceptor should store a public property to the original?

    Code:
    Ext.apply(Function.prototype, {
        createInterceptor : function(fcn, scope){
            var method = this, result = Ext.isFunction(fcn) ?
                    this :
                    function() {
                        var me = this,
                            args = arguments;
                        fcn.target = me;
                        fcn.method = method;
                        return (fcn.apply(scope || me || window, args) !== false) ?
                                method.apply(me || window, args) :
                                null;
                    };
            result.original = method;
            return result;
        }
    });

  4. #4
    Sencha User steffenk's Avatar
    Join Date
    Jul 2007
    Location
    Haan, Germany
    Posts
    2,649
    Vote Rating
    6
    steffenk has a spectacular aura about steffenk has a spectacular aura about steffenk has a spectacular aura about

      0  

    Default


    Hi Animal,

    yes, this is exact what is needed. If we have the reference we can remove it.

    Let's take an example with eventListener. I tried to store the original in a var and tried to remove it later, but had no success

    Code:
    Ext.myTest = function() {
       var oldFunction = null;
    	
    	return {
    		/* Event monitoring */
    		startEventMonitoring: function() {
    			Ext.myTest.oldFunction = Ext.util.Observable.prototype.fireEvent;
    			Ext.util.Observable.prototype.fireEvent = Ext.util.Observable.prototype.fireEvent.createInterceptor(function() {
    				console.log(arguments);
    				return true;
    			});
    		},
    	
    		stopEventMonitoring: function() {
    			if (Ext.myTest.oldFunction) {
    				Ext.util.Observable.prototype.removeMethodListener(Ext.myTest.oldFunction, Ext.util.Observable.prototype.fireEvent);
    				Ext.myTest.oldFunction = null;
    			}
    		}
    }();
    I tried your snippet, but then interCeptor isn't created, though i have the original method in Ext.util.Observable.prototype.fireEvent.original
    vg Steffen
    --------------------------------------
    Release Manager of TYPO3 4.5

  5. #5
    Sencha User steffenk's Avatar
    Join Date
    Jul 2007
    Location
    Haan, Germany
    Posts
    2,649
    Vote Rating
    6
    steffenk has a spectacular aura about steffenk has a spectacular aura about steffenk has a spectacular aura about

      0  

    Default


    I'm bit astonished that noone is interested in. Don't you use interceptors, or only create without want to remove? Does noone work with chains?

    As ExtJS event-driven perspective is a really important one i would love to have an API for this event chains.
    Experts?
    vg Steffen
    --------------------------------------
    Release Manager of TYPO3 4.5

  6. #6
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,483
    Vote Rating
    35
    Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    I use lots of interceptors. If I ever needed only the original function again, then I'd save a reference to it.

  7. #7
    Sencha User steffenk's Avatar
    Join Date
    Jul 2007
    Location
    Haan, Germany
    Posts
    2,649
    Vote Rating
    6
    steffenk has a spectacular aura about steffenk has a spectacular aura about steffenk has a spectacular aura about

      0  

    Default


    Hi Animal, i hoped you answer
    Would you like to share how you save the reference and how you restore?
    Thanks!
    vg Steffen
    --------------------------------------
    Release Manager of TYPO3 4.5

  8. #8
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,483
    Vote Rating
    35
    Animal has a spectacular aura about Animal has a spectacular aura about

      0  

    Default


    You can restore the state of the class's prototype by keeping a reference to the original function, and poking it back in.

    Of course if, while the prototype was in a modified state, you had used that member as an event listener fn, the listener would be holding a reference to the sequence, so you could not change that. You'd have to remove and re-add the listener.

    But restoring the prototype is simple. Just poke the function reference back in.

  9. #9
    Sencha User renku's Avatar
    Join Date
    Feb 2009
    Location
    Estonia
    Posts
    433
    Vote Rating
    17
    renku is a splendid one to behold renku is a splendid one to behold renku is a splendid one to behold renku is a splendid one to behold renku is a splendid one to behold renku is a splendid one to behold renku is a splendid one to behold

      0  

    Default


    When you create a function sequence using either .createSequence() or .createInterceptor(), removing functions from that sequence is not really possible. The only thing you can do is to substitute back the original function as Animal described.

    Code:
    // the function to extend
    var foo = function(){alert("bar");};
    
    // extending foo with bar
    var bar = function(){alert("bar");};
    var oldFoo = foo;
    foo = foo.createSequence(bar);
    
    // somewhere later to restore the original
    foo = oldFoo;
    But this might not always be the safe thing to do. When some other part of the program also modifies the foo in the meantime:

    Code:
    var baz = function(){alert("baz");};
    foo = foo.createSequence(baz);
    Then restoring the foo to its original state will erase that other modification.

    I wrote a small code to demonstrate how it could be done to allow removing stuff from created function-sequences:

    Code:
    Function.prototype.addHook = function(func) {
      if (!this.__hooks) {
        var seq = function() {
          for (var i=0; i<seq.__hooks.length; i++) {
            seq.__hooks[i].apply(arguments);
          }
        };
        seq.__hooks = [this, func];
        return seq;
      }
      else {
        this.__hooks.push(func);
        return this;
      }
    };
    
    Function.prototype.removeHook = function(func) {
      if (this.__hooks) {
        this.__hooks.remove(func);
      }
      return this;
    };
    
    
    var foo = function(){alert("foo");};
    var bar = function(){alert("bar");};
    var baz = function(){alert("baz");};
    
    var foobarbaz = foo.addHook(bar).addHook(baz);
    foobarbaz(); // alerts foo, bar, baz
    
    var foobaz = foobarbaz.removeHook(bar);
    foobarbaz(); // alerts foo, baz
    But the morale of the story is: if you need to do that kind of stuff, you should use events instead.

  10. #10
    Sencha User steffenk's Avatar
    Join Date
    Jul 2007
    Location
    Haan, Germany
    Posts
    2,649
    Vote Rating
    6
    steffenk has a spectacular aura about steffenk has a spectacular aura about steffenk has a spectacular aura about

      0  

    Default


    Hi renku,

    thanks for sharing your idea.

    i also think that the stack of functions should be saved internal. Animal suggested to return the reference, but then you have to manage all yourself. Some functions also would be nice like
    Code:
    Function.prototype.getHooks = function() {
      return this.__hooks;
    };
    Function.prototype.removeAllHooks = function() {
      this.__hooks = shift(this.__hooks);
    };
    Why is your morale like this? I think this is useful for hook in functions, and it is good to have a simple API to use.
    vg Steffen
    --------------------------------------
    Release Manager of TYPO3 4.5

Thread Participants: 2

film izle

hd film izle

film sitesi

takipci kazanma sitesi

takipci kazanma sitesi

güzel olan herşey

takipci alma sitesi

komik eğlenceli videolar