Tuesday, August 9, 2011

HierarchicalCollectionView and Sorting Discovery

More woes around HierarchicalCollectionView. This was discovered while trying sort from the columns in the AdvancedDataGrid.

The usual case is that you click on a column and the sorting gets applied based on that column's datafield. In our case though, the datafield was a placeholder, as the item renderer took care of collecting the needed data out of the local cache based on the materialId.

Soooo... for sorting, we applied a sortCompareFunction to the column to account for this - so far so good.... until.....

 private function sortCanBeApplied(coll:ICollectionView):Boolean
        if (sort == null)
            return true;
        // get the current item
        var obj:Object = coll.createCursor().current;
        if (!obj || !sort.fields)
            return false;
        // check for the properties (sort fields) in the current object
        for (var i:int = 0; i < sort.fields.length; i++)
            var sf:SortField = sort.fields[i];
            if (!obj.hasOwnProperty(sf.name))
                return false;
        return true;
Notice line 16: if (!obj.hasOwnProperty(sf.name)) Well, if you trace everything back, it is essentially saying that the data HAS to have the property of the datafield of the column, otherwise no sorting.

Drat... now we have to add all of these [Transient] property placeholders to our vos, just to get sorting to work. (While messy, the other alternatives seemed even more messy and risky. Please comment if you have suggestions).

Here is a list of ideas that we considered but decided that it would make the code base less readable and understandable

  • Override hasOwnProperty on the VO
  • Override AdvancedDataGrid.addSortField() to change the sortField.name