FLEX-34884
CAUSE
The reason items weren't findable in a collection sorted by complex fields was 
that Sort.findItem() was going through the field names to make sure that all of 
them are present on the destination objects. This check failed for complex 
objects.

SOLUTION
Instead of checking by itself, Sort.findItem() now delegates this 
responsibility to the respective ISortFields. In turn, SortField and 
ComplexSortField can now tell Sort whether an object has a certain property 
and, respectively, whether an object has the first property in the chain for 
ComplexSortField (because any part of the chain can be null without meaning the 
entire chain can never be accessed - it just can't be accessed now).

NOTES
-Now the previously added unit test passes.
-Also note that in this way Sort.fieldList is no longer needed, which should 
speed up the fields setter.


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

Branch: refs/heads/develop
Commit: 047017398a2ccea7ddb2b0300ec4a184ad7f9ab6
Parents: 418359c
Author: Mihai Chira <[email protected]>
Authored: Mon Jun 8 16:19:06 2015 +0200
Committer: Mihai Chira <[email protected]>
Committed: Mon Jun 8 16:19:06 2015 +0200

----------------------------------------------------------------------
 .../src/mx/collections/ComplexSortField.as      |  8 +++-
 .../framework/src/mx/collections/ISortField.as  | 14 +++++++
 .../framework/src/mx/collections/Sort.as        | 40 +++++---------------
 .../framework/src/mx/collections/SortField.as   | 10 ++++-
 4 files changed, 37 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/04701739/frameworks/projects/framework/src/mx/collections/ComplexSortField.as
----------------------------------------------------------------------
diff --git 
a/frameworks/projects/framework/src/mx/collections/ComplexSortField.as 
b/frameworks/projects/framework/src/mx/collections/ComplexSortField.as
index 0736be3..1bc5f6d 100644
--- a/frameworks/projects/framework/src/mx/collections/ComplexSortField.as
+++ b/frameworks/projects/framework/src/mx/collections/ComplexSortField.as
@@ -22,7 +22,6 @@ package mx.collections {
 
     public class ComplexSortField extends SortField
     {
-
         private var _nameParts:Array;
 
         public function ComplexSortField(name:String = null,
@@ -34,7 +33,12 @@ package mx.collections {
             _nameParts = name.split(".");
         }
 
-        override protected function getSortFieldValue(obj:Object):Object
+        override public function objectHasSortField(object:Object):Boolean
+        {
+            return object && _nameParts && _nameParts.length && 
object.hasOwnProperty(_nameParts[0]);
+        }
+
+        override protected function getSortFieldValue(obj:Object):*
         {
             return ObjectUtil.getValue(obj, _nameParts);
         }

http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/04701739/frameworks/projects/framework/src/mx/collections/ISortField.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/framework/src/mx/collections/ISortField.as 
b/frameworks/projects/framework/src/mx/collections/ISortField.as
index 8ce3228..766bd69 100644
--- a/frameworks/projects/framework/src/mx/collections/ISortField.as
+++ b/frameworks/projects/framework/src/mx/collections/ISortField.as
@@ -254,5 +254,19 @@ public interface ISortField
      *  @productversion Flex 4.11
      */
     function updateSortCompareType():Boolean;
+
+    /**
+     *  Returns true if the object has the field required by this 
<code>ISortField</code> instance.
+     *  In  the case of <code>ComplexSortField</code>, returns true if the 
object has a field with
+     *  an identical name to the first part of the <code>namePath</code>.
+     *
+     *  @return true if the object has the field required by this 
<code>ISortField</code> instance.
+     *
+     *  @langversion 3.0
+     *  @playerversion Flash 11.8
+     *  @playerversion AIR 3.8
+     *  @productversion Flex 4.15
+     */
+    function objectHasSortField(object:Object):Boolean;
 }
 }

http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/04701739/frameworks/projects/framework/src/mx/collections/Sort.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/framework/src/mx/collections/Sort.as 
b/frameworks/projects/framework/src/mx/collections/Sort.as
index fa52f2f..162dad0 100644
--- a/frameworks/projects/framework/src/mx/collections/Sort.as
+++ b/frameworks/projects/framework/src/mx/collections/Sort.as
@@ -265,11 +265,6 @@ public class Sort extends EventDispatcher implements ISort
      */
     private var _fields:Array;
 
-    /**
-     *  @private
-     */
-    private var fieldList:Array = [];
-
     [Inspectable(category="General", arrayType="mx.collections.ISortField")]
     [Bindable("fieldsChanged")]
 
@@ -296,16 +291,7 @@ public class Sort extends EventDispatcher implements ISort
     public function set fields(value:Array):void
     {
         _fields = value;
-        fieldList = [];
-        if (_fields)
-        {
-            var field:ISortField;
-            for (var i:int = 0; i<_fields.length; i++)
-            {
-                field = ISortField(_fields[i]);
-                fieldList.push(field.name);
-            }
-        }
+
         dispatchEvent(new Event("fieldsChanged"));
     }
 
@@ -399,38 +385,30 @@ public class Sort extends EventDispatcher implements ISort
         {
             compareForFind = this.compareFunction;
             // configure the search criteria
-            if (values && fieldList.length > 0)
+            if (values && fields && fields.length > 0)
             {
                 fieldsForCompare = [];
                 //build up the fields we can compare, if we skip a field in the
                 //middle throw an error.  it is ok to not have all the fields
                 //though
-                var fieldName:String;
+                var field:ISortField;
                 var hadPreviousFieldName:Boolean = true;
-                for (var i:int = 0; i < fieldList.length; i++)
+                for (var i:int = 0; i < fields.length; i++)
                 {
-                    fieldName = fieldList[i];
-                    if (fieldName)
+                    field = fields[i] as ISortField;
+                    if (field.name)
                     {
-                        var hasFieldName:Boolean = false;     
-                                               try
-                        {
-                            hasFieldName = values[fieldName] !== undefined;
-                        }
-                        catch(e:Error)
-                        {
-                        }
-                        if (hasFieldName)
+                        if (field.objectHasSortField(values))
                         {
                             if (!hadPreviousFieldName)
                             {
                                 message = resourceManager.getString(
-                                    "collections", "findCondition", [ 
fieldName ]);
+                                    "collections", "findCondition", 
[field.name]);
                                 throw new SortError(message);
                             }
                             else
                             {
-                                fieldsForCompare.push(fieldName);
+                                fieldsForCompare.push(field.name);
                             }
                         }
                         else

http://git-wip-us.apache.org/repos/asf/flex-sdk/blob/04701739/frameworks/projects/framework/src/mx/collections/SortField.as
----------------------------------------------------------------------
diff --git a/frameworks/projects/framework/src/mx/collections/SortField.as 
b/frameworks/projects/framework/src/mx/collections/SortField.as
index 71712ea..6604720 100644
--- a/frameworks/projects/framework/src/mx/collections/SortField.as
+++ b/frameworks/projects/framework/src/mx/collections/SortField.as
@@ -660,15 +660,21 @@ public class SortField extends EventDispatcher implements 
ISortField
     }
 
 
+    public function objectHasSortField(object:Object):Boolean
+    {
+        return getSortFieldValue(object) !== undefined;
+    }
+
+
     
//--------------------------------------------------------------------------
     //
     //  Protected Methods
     //
     
//--------------------------------------------------------------------------
 
-    protected function getSortFieldValue(obj:Object):Object
+    protected function getSortFieldValue(obj:Object):*
     {
-        var result:Object = null;
+        var result:Object = undefined;
 
         try
         {

Reply via email to