views:

626

answers:

5

Hi friends, I want to check in my function if a passed argument of type object is empty or not. Sometimes it is empty but still not null thus I can not rely on null condition. Is there some property like 'length'/'size' for flex objects which I can use here. Please help. Thanks in advance.

+6  A: 

If you mean if an Object has no properties:

var isEmpty:Boolean = true;
for (var n in obj) { isEmpty = false; break; }
Simon Buchan
Thanks for the help, it was really useful but still can't we have some builtin method like :"myObj.length"/'myObj.isEmpty' Thanks again.
Ashine
You are gonna have to extend the `Object` class or the `Dictionary` class to get an "inbuilt" method for that :)
Amarghosh
There is monkeypatching, but that breaks for .. in loops, which... well... you know.
Simon Buchan
+4  A: 

This is some serious hack but you can use:

Object.prototype.isEmpty = function():Boolean {
    for(var i in this)
     if(i != "isEmpty")
      return false
    return true
}

var p = {};
trace(p.isEmpty()); // true
var p2 = {a:1}
trace(p2.isEmpty()); // false
sharvey
wow, that's an interesting trick. You just sent me on a quest to learn about the prototype member. Not sure how I might use it, but it's an interesting bit of guts to know about.
JStriedl
I try to avoid prototypes, because the flex compiler complains alot. And I must admit that I'm an auto-completion addict...
sharvey
possibly check using `this.hasOwnProperty(i)`, in case of other prototype members.
Simon Buchan
A: 

Depends on what your object is, or rather what you expect it to have. For example if your object is supposed to contain some property called name that you are looking for, you might do

if(objSomeItem == null || objSomeItem.name == null || objSomeItem.name.length == 0)
{
 trace("object is empty");
}

or if your object is actually supposed to be something else, like an array you could do

var arySomeItems = objSomeItem as Array;
if(objSomeItem == null || arySomeItems == null || arySomeItems.length == 0)
{
  trace("object is empty");
}

You could also use other ways through reflection, such as ObjectUtil.getClassInfo, then enumerate through the properties to check for set values.... this class help:

import flash.utils.describeType;
import flash.utils.getDefinitionByName;

public class ReflectionUtils 
{
 /** Returns an Array of All Properties of the supplied object */
 public static function GetVariableNames(objItem:Object):Array
 {
  var xmlPropsList:XMLList = describeType(objItem)..variable;
  var aryVariables:Array = new Array();
  if (xmlPropsList != null)
  {
   for (var i:int; i < xmlPropsList.length(); i++)
   {
    aryVariables.push(xmlPropsList[i].@name);
   }
  }

  return aryVariables;
 }

 /** Returns the Strongly Typed class of the specified library item */
 public static function GetClassByName($sLinkageName:String):Class
 {
  var tObject:Class = getDefinitionByName($sLinkageName) as Class;
  return tObject;
 }

 /** Constructs an instance of the speicified library item */
 public static function ConstructClassByName($sLinkageName:String):Object
 {
  var tObject:Class = GetClassByName($sLinkageName);
  //trace("Found Class: " + tMCDefinition);
  var objItem:* = new tObject();
  return objItem;
 }

 public static function DumpObject(sItemName:String, objItem:Object):void
 {
  trace("*********** Object Dump: " + sItemName + " ***************");
  for (var sKey:String in objItem)
  {
   trace("    " + sKey +": " + objItem[sKey]);
  }
 }
 //}
}

Another thing to note is you can use a simple for loop to check through an objects properties, thats what this dumpobject function is doing.

JTtheGeek
A: 

can use use the hasProperty method to check for length

var i:int = myObject.hasProperty("length") ? myObject.length: 0;
AndrewB
His problem is that there's no length field on dynamic objects.
sharvey
The way I read it is that he did not know if there were a length property or not.
AndrewB
+1  A: 

You can also try:

ObjectUtil.getClassInfo(obj).properties.length > 0

The good thing about it is that getClassInfo gives you much more info about the object, eg. you get the names of all the properties in the object, which might come in handy.

Robert Bak