You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flex.apache.org by ah...@apache.org on 2015/01/13 19:11:13 UTC

[04/17] git commit: [flex-asjs] [refs/heads/develop] - add support for binding to longer chains of properties

add support for binding to longer chains of properties


Project: http://git-wip-us.apache.org/repos/asf/flex-asjs/repo
Commit: http://git-wip-us.apache.org/repos/asf/flex-asjs/commit/a3e7664b
Tree: http://git-wip-us.apache.org/repos/asf/flex-asjs/tree/a3e7664b
Diff: http://git-wip-us.apache.org/repos/asf/flex-asjs/diff/a3e7664b

Branch: refs/heads/develop
Commit: a3e7664b298b286def0fcd9637c9dade0d3160ad
Parents: add4e92
Author: Alex Harui <ah...@apache.org>
Authored: Tue Jan 13 10:03:22 2015 -0800
Committer: Alex Harui <ah...@apache.org>
Committed: Tue Jan 13 10:03:22 2015 -0800

----------------------------------------------------------------------
 .../apache/flex/core/ContainerDataBinding.as    |  18 +-
 .../src/org/apache/flex/binding/ChainBinding.as | 220 +++++++++++++++++++
 .../src/org/apache/flex/binding/ChainBinding.js | 220 +++++++++++++++++++
 3 files changed, 455 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/a3e7664b/frameworks/as/projects/FlexJSJX/src/org/apache/flex/core/ContainerDataBinding.as
----------------------------------------------------------------------
diff --git a/frameworks/as/projects/FlexJSJX/src/org/apache/flex/core/ContainerDataBinding.as b/frameworks/as/projects/FlexJSJX/src/org/apache/flex/core/ContainerDataBinding.as
index 28e6c59..12c46d9 100644
--- a/frameworks/as/projects/FlexJSJX/src/org/apache/flex/core/ContainerDataBinding.as
+++ b/frameworks/as/projects/FlexJSJX/src/org/apache/flex/core/ContainerDataBinding.as
@@ -18,6 +18,7 @@
 ////////////////////////////////////////////////////////////////////////////////
 package org.apache.flex.core
 {
+    import org.apache.flex.binding.ChainBinding;
     import org.apache.flex.binding.ConstantBinding;
     import org.apache.flex.binding.GenericBinding;
     import org.apache.flex.binding.PropertyWatcher;
@@ -100,13 +101,14 @@ package org.apache.flex.core
                 {
                     if (hasProperty(_strand, binding.source[0]))
                     {
+                        var destObject:Object;
+                        var destination:IStrand;
+                        var compWatcher:Object;
                         if (binding.source.length == 2 && binding.destination.length == 2)
                         {
                             // simple component.property binding
-                            var destObject:Object;
-                            var destination:IStrand;
                             // can be simplebinding or constantbinding
-                            var compWatcher:Object = watchers.watcherMap[binding.source[0]];
+                            compWatcher = watchers.watcherMap[binding.source[0]];
                             fieldWatcher = compWatcher.children.watcherMap[binding.source[1]];
                             if (fieldWatcher.eventNames is String)
                             {
@@ -160,6 +162,16 @@ package org.apache.flex.core
                                 }
                             }
                         }
+                        else
+                        {
+                            compWatcher = watchers.watcherMap[binding.source[0]];
+                            var chb:ChainBinding = new ChainBinding();
+                            chb.destination = binding.destination;
+                            chb.source = binding.source;
+                            chb.watcherChain = compWatcher;
+                            chb.setDocument(_strand);
+                            _strand.addBead(chb);
+                        }
                     }
                 }
                 else if (binding.source is String && binding.destination is Array)

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/a3e7664b/frameworks/as/projects/FlexJSUI/src/org/apache/flex/binding/ChainBinding.as
----------------------------------------------------------------------
diff --git a/frameworks/as/projects/FlexJSUI/src/org/apache/flex/binding/ChainBinding.as b/frameworks/as/projects/FlexJSUI/src/org/apache/flex/binding/ChainBinding.as
new file mode 100644
index 0000000..207246a
--- /dev/null
+++ b/frameworks/as/projects/FlexJSUI/src/org/apache/flex/binding/ChainBinding.as
@@ -0,0 +1,220 @@
+////////////////////////////////////////////////////////////////////////////////
+//
+//  Licensed to the Apache Software Foundation (ASF) under one or more
+//  contributor license agreements.  See the NOTICE file distributed with
+//  this work for additional information regarding copyright ownership.
+//  The ASF licenses this file to You under the Apache License, Version 2.0
+//  (the "License"); you may not use this file except in compliance with
+//  the License.  You may obtain a copy of the License at
+//
+//      http://www.apache.org/licenses/LICENSE-2.0
+//
+//  Unless required by applicable law or agreed to in writing, software
+//  distributed under the License is distributed on an "AS IS" BASIS,
+//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+//  See the License for the specific language governing permissions and
+//  limitations under the License.
+//
+////////////////////////////////////////////////////////////////////////////////
+package org.apache.flex.binding
+{	
+	import flash.events.Event;
+	import flash.events.IEventDispatcher;
+	
+	import org.apache.flex.core.IBead;
+	import org.apache.flex.core.IDocument;
+	import org.apache.flex.core.IStrand;
+	import org.apache.flex.events.ValueChangeEvent;
+
+    /**
+     *  The SimpleBinding class is lightweight data-binding class that
+     *  is optimized for simple assignments of one object's property to
+     *  another object's property.
+     *  
+     *  @langversion 3.0
+     *  @playerversion Flash 10.2
+     *  @playerversion AIR 2.6
+     *  @productversion FlexJS 0.0
+     */
+	public class ChainBinding implements IBead, IDocument
+	{
+        /**
+         *  Constructor.
+         *  
+         *  @langversion 3.0
+         *  @playerversion Flash 10.2
+         *  @playerversion AIR 2.6
+         *  @productversion FlexJS 0.0
+         */
+		public function ChainBinding()
+		{
+		}
+		
+        /**
+         *  The source chain of property names
+         *
+         *  @langversion 3.0
+         *  @playerversion Flash 10.2
+         *  @playerversion AIR 2.6
+         *  @productversion FlexJS 0.0
+         */
+		public var source:Array;
+
+        /**
+         *  The host mxml document for the source and
+         *  destination objects.  The source object
+         *  is either this document for simple bindings
+         *  like {foo} where foo is a property on
+         *  the mxml documnet, or found as document[sourceID]
+         *  for simple bindings like {someid.someproperty}
+         *
+         *  @langversion 3.0
+         *  @playerversion Flash 10.2
+         *  @playerversion AIR 2.6
+         *  @productversion FlexJS 0.0
+         */
+        protected var document:Object;
+
+        /**
+         *  The destination property name or chain.
+         *
+         *  @langversion 3.0
+         *  @playerversion Flash 10.2
+         *  @playerversion AIR 2.6
+         *  @productversion FlexJS 0.0
+         */
+		public var destination:Object;
+        
+        /**
+         *  The watcher for the component at
+         *  document[source[0]]
+         *
+         *  @langversion 3.0
+         *  @playerversion Flash 10.2
+         *  @playerversion AIR 2.6
+         *  @productversion FlexJS 0.0
+         */
+		public var watcherChain:Object;
+        // TODO: (aharui) handle watcher chain
+        
+        private var value:Object;
+        
+        /**
+         *  @copy org.apache.flex.core.IBead#strand
+         *
+         *  @langversion 3.0
+         *  @playerversion Flash 10.2
+         *  @playerversion AIR 2.6
+         *  @productversion FlexJS 0.0
+         */
+		public function set strand(value:IStrand):void
+		{
+            applyBinding();
+        }
+        
+        private function applyBinding():void
+        {
+            var chainSet:Boolean = evaluateSourceChain();
+            if (chainSet)
+                applyValue();
+        }
+        
+        private function evaluateSourceChain():Boolean
+        {
+            var n:int = source.length;
+            var obj:Object = document;
+            for (var i:int = 0; i < n - 1; i++)
+            {
+                var propName:String = source[i];
+                var propObj:Object = obj[propName];
+                var watcher:ChainWatcher = new ChainWatcher(propName, applyBinding);
+                obj.addEventListener("valueChange", watcher.handler);
+                if (propObj == null)
+                    return false;
+                obj = propObj;
+            }
+            propName = source[n - 1];
+            function valueChangeHandler(event:ValueChangeEvent):void {
+                if (event.propertyName != propName)
+                    return;
+                value = event.newValue;
+                applyValue();
+            }
+            obj.addEventListener("valueChange", valueChangeHandler);
+            
+            // we have a complete chain, get the value
+            value = obj[propName];
+            return true;
+        }
+        
+        private function applyValue():void
+        {
+            var destinationName:String;
+            if (destination is String)
+            {
+                destinationName = destination as String;
+                document[destinationName] = value;
+                return;
+            }
+
+            var n:int = destination.length;
+            var obj:Object = document;
+            for (var i:int = 0; i < n - 1; i++)
+            {
+                var propName:String = destination[i];
+                var propObj:Object = obj[propName];
+                function handler(event:ValueChangeEvent):void {
+                    if (event.propertyName != propName)
+                        return;
+                    if (event.oldValue != null)
+                        event.oldValue.removeEventListener("valueChange", handler);
+                    applyValue();
+                }
+                if (propObj == null)
+                {
+                    obj.addEventListener("valueChange", handler);
+                    return;
+                }
+                obj = propObj;
+            }
+            obj[destination[n - 1]] = value;
+            
+		}
+		
+        /**
+         *  @copy org.apache.flex.core.IDocument#setDocument()
+         *
+         *  @langversion 3.0
+         *  @playerversion Flash 10.2
+         *  @playerversion AIR 2.6
+         *  @productversion FlexJS 0.0
+         */
+		public function setDocument(document:Object, id:String = null):void
+		{
+			this.document = document;
+		}
+	}
+}
+
+import org.apache.flex.events.ValueChangeEvent;
+
+class ChainWatcher
+{
+    public function ChainWatcher(propertyName:String, callback:Function)
+    {
+        this.propertyName = propertyName;
+        this.callback = callback;
+    }
+
+    private var propertyName:String;
+    private var callback:Function;
+    
+    public function handler(event:ValueChangeEvent):void
+    {
+        if (event.propertyName != propertyName)
+            return;
+        if (event.oldValue != null)
+            event.oldValue.removeEventListener("valueChange", handler);
+        callback();               
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/flex-asjs/blob/a3e7664b/frameworks/js/FlexJS/src/org/apache/flex/binding/ChainBinding.js
----------------------------------------------------------------------
diff --git a/frameworks/js/FlexJS/src/org/apache/flex/binding/ChainBinding.js b/frameworks/js/FlexJS/src/org/apache/flex/binding/ChainBinding.js
new file mode 100644
index 0000000..002555f
--- /dev/null
+++ b/frameworks/js/FlexJS/src/org/apache/flex/binding/ChainBinding.js
@@ -0,0 +1,220 @@
+/**
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+goog.provide('org.apache.flex.binding.ChainBinding');
+goog.provide('org.apache.flex.binding.ChainWatcher');
+
+
+
+/**
+ * @constructor
+ */
+org.apache.flex.binding.ChainBinding = function() {
+};
+
+
+/**
+ * Metadata
+ *
+ * @type {Object.<string, Array.<Object>>}
+ */
+org.apache.flex.binding.ChainBinding.prototype.FLEXJS_CLASS_INFO =
+    { names: [{ name: 'ChainBinding',
+                qName: 'org.apache.flex.binding.ChainBinding'}] };
+
+
+/**
+ * @protected
+ * @type {Object}
+ */
+org.apache.flex.binding.ChainBinding.prototype.document = null;
+
+
+/**
+ * @expose
+ * @type {Object}
+ */
+org.apache.flex.binding.ChainBinding.prototype.destination = null;
+
+
+/**
+ * @expose
+ * @type {Object}
+ */
+org.apache.flex.binding.ChainBinding.prototype.value = null;
+
+
+/**
+ * @expose
+ * @type {Object}
+ */
+org.apache.flex.binding.ChainBinding.prototype.source = null;
+
+
+/**
+ * @expose
+ * @param {Object} value The strand (owner) of the bead.
+ */
+org.apache.flex.binding.ChainBinding.prototype.set_strand = function(value) {
+  this.applyBinding();
+};
+
+
+/**
+ */
+org.apache.flex.binding.ChainBinding.prototype.applyBinding = function() {
+  var chainSet = this.evaluateSourceChain();
+  if (chainSet)
+    this.applyValue();
+};
+
+
+/**
+ * @return {boolean} True if chain complete.
+ */
+org.apache.flex.binding.ChainBinding.prototype.evaluateSourceChain = function() {
+  var propName;
+  var n = this.source.length;
+  var obj = this.document;
+  for (var i = 0; i < n - 1; i++) {
+    propName = this.source[i];
+    var propObj;
+    if (typeof(obj['get_' + propName]) === 'function')
+      propObj = obj['get_' + propName]();
+    else
+      propObj = obj[propName];
+    var watcher = new org.apache.flex.binding.ChainWatcher(propName, this.applyBinding);
+    obj.addEventListener('valueChange', watcher.handler);
+    if (propObj == null)
+      return false;
+    obj = propObj;
+  }
+  propName = this.source[n - 1];
+  var self = this;
+  function valueChangeHandler(event) {
+    if (event.propertyName != propName)
+      return;
+    self.value = event.newValue;
+    self.applyValue();
+  }
+  obj.addEventListener('valueChange', valueChangeHandler);
+
+  // we have a complete chain, get the value
+  if (typeof(obj['get_' + propName]) === 'function')
+    this.value = obj['get_' + propName]();
+  else
+    this.value = obj[propName];
+  return true;
+};
+
+
+/**
+ */
+org.apache.flex.binding.ChainBinding.prototype.applyValue = function() {
+  var destinationName;
+  if (typeof(this.destination) === 'string') {
+    destinationName = this.destination;
+    if (typeof(this.document['set_' + destinationName]) === 'function')
+      this.document['set_' + destinationName](this.value);
+    else
+      this.document[destinationName] = this.value;
+    return;
+  }
+
+  var n = this.destination.length;
+  var obj = this.document;
+  var self = this;
+  for (var i = 0; i < n - 1; i++) {
+    var propName = this.destination[i];
+    var propObj;
+    if (typeof(obj['get_' + propName]) === 'function')
+      propObj = obj['get_' + propName]();
+    else
+      propObj = obj[propName];
+    function handler(event) {
+      if (event.propertyName != propName)
+        return;
+      if (event.oldValue != null)
+        event.oldValue.removeEventListener('valueChange', handler);
+      self.applyValue();
+    }
+    if (propObj == null) {
+      obj.addEventListener('valueChange', handler);
+      return;
+    }
+    obj = propObj;
+  }
+  if (typeof(obj['set_' + this.destination[n - 1]]) === 'function')
+    obj['set_' + this.destination[n - 1]](this.value);
+  else
+    obj[this.destination[n - 1]] = this.value;
+};
+
+
+/**
+ * @expose
+ * @param {Object} document The MXML object.
+ */
+org.apache.flex.binding.ChainBinding.prototype.setDocument = function(document) {
+  this.document = document;
+};
+
+
+
+/**
+ * @constructor
+ * @param {string} propName The name of the property to watch.
+ * @param {function()} cb The callback function.
+ */
+org.apache.flex.binding.ChainWatcher = function(propName, cb) {
+  this.propertyName = propName;
+  this.callback = cb;
+};
+
+
+/**
+ * Metadata
+ *
+ * @type {Object.<string, Array.<Object>>}
+ */
+org.apache.flex.binding.ChainWatcher.prototype.FLEXJS_CLASS_INFO =
+    { names: [{ name: 'ChainWatcher',
+                qName: 'org.apache.flex.binding.ChainWatcher'}] };
+
+
+/**
+ * @protected
+ * @type {string}
+ */
+org.apache.flex.binding.ChainWatcher.prototype.propertyName = '';
+
+
+/**
+ * @protected
+ * @type {?function()}
+ */
+org.apache.flex.binding.ChainWatcher.prototype.callback = null;
+
+
+/**
+ * @expose
+ * @param {Object} event The event object.
+ */
+org.apache.flex.binding.ChainWatcher.prototype.handler = function(event) {
+  if (event.propertyName != this.propertyName)
+    return;
+  if (event.oldValue != null)
+    event.oldValue.removeEventListener('valueChange', this.handler);
+  this.callback();
+};