Tuesday, March 11, 2014

How can we remove binding on Object in Flex?

Binding is one important feature of Flex. There are various ways to achieve it. If we use BindingUtils then we get an instance of ChangeWatcher class.
var watcherSetter:ChangeWatcher = 
                    BindingUtils.bindSetter(updateMyString, myTI, "text");
This class (ChangeWatcher) provides method unwatch() to remove the binding. We can also use ChangeWatcher directly as well.
var canWatch:Boolean = ChangeWatcher.canWatch(myObject, 'myProperty');
ChangeWatcher.watch(myObject, 'myProperty', myPropertyChangedHandler) 
BindingUtils is more like a wrapper over it and internally this class make use of ChangeWatcher only. Consider the bindProperty method:
public static function bindProperty(
                                site:Object, prop:String,
                                host:Object, chain:Object,
                                commitOnly:Boolean = false,
                               useWeakReference:Boolean = false):ChangeWatcher
{
    var w:ChangeWatcher =
       ChangeWatcher.watch(host, chain, null, commitOnly, useWeakReference);
        
    if (w != null)
    {
       var assign:Function = function(event:*):void
       {
          site[prop] = w.getValue();
       };
       w.setHandler(assign);
       assign(null);
    }
    return w;
}
and bindSetter method:
public static function bindSetter(setter:Function, host:Object,
                               chain:Object,
                               commitOnly:Boolean = false,
                              useWeakReference:Boolean = false):ChangeWatcher
{
    var w:ChangeWatcher =
       ChangeWatcher.watch(host, chain, null, commitOnly, useWeakReference);
        
    if (w != null)
    {
       var invoke:Function = function(event:*):void
       {
         setter(w.getValue());
       };
       w.setHandler(invoke);
       invoke(null);
    }
    return w;
}
Now the question asked now a days is how can we remove binding on an object? Before answering that lets try to understand what happens in Binding class:
public function Binding(document:Object, srcFunc:Function,
  			        destFunc:Function, destString:String,
				srcString:String = null)
    {
		super();

        this.document = document;
        this.srcFunc = srcFunc;
        this.destFunc = destFunc;
        this.destString = destString;
        this.srcString = srcString;

        if (this.srcFunc == null)
        {
            this.srcFunc = defaultSrcFunc;
        }

        if (this.destFunc == null)
        {
            this.destFunc = defaultDestFunc;
        }

        _isEnabled = true;
        isExecuting = false;
        isHandlingEvent = false;
        hasHadValue = false;
        uiComponentWatcher = -1;

        BindingManager.addBinding(document, destString, this);
    }   
In the constructor it sets various values and then makes call to addBinding() method in BindingManager class.
 public static function addBinding(document:Object, destStr:String,							  b:Binding):void
{
   if (!document._bindingsByDestination)
   {
      document._bindingsByDestination = {};
      document._bindingsBeginWithWord = {};
   }
   document._bindingsByDestination[destStr] = b;
   document._bindingsBeginWithWord[getFirstWord(destStr)] = true;
}

Actually all the bindings are stored in the following arrays:
  • mx_internal _bindings:Array
  • mx_internal _watchers:Array
  • mx_internal _bindingsByDefinitions
  • mx_internal _bindingsBeginWithWord
To remove binding we can use the following method as explained here:
public function removeBindings(object:Object):void
{
   if(!object) return
   if(object._bindings)
   {  
      for(var i:int = 0; i lt object._bindings.length; i++)
        object.bindings[i]= null;
      object._bindings.length = 0;
      object._bindings = null;
   }
   if(object._watchers)
   {
       for(i=0; i lt object._watchers.length; i++)
         object._watchers[i]= null;
       object._watchers.length = 0;
       object._watchers = null;
   }
   if(object._bindingsByDestination)
     object._bindingsByDestination = null;
   if(object._bindingsBeginWithWord
     object._bindingsBeginWithWord = null;
} 
I have used lt for less-than operator as it was getting formatting issues.

That is all and I hope you have enjoyed this post.

No comments: