views:

42

answers:

2

If I want to know if an object has a particular property I can code this:

if (SomeObject.hasOwnProperty('xyz')) {
  // some code
}

But some styles masquerade as properties at design time such as Button.color... How can I know what style properties are valid at runtime? ie: What is the equivalent of hasOwnProperty for getStyle/setStyle?

In other words how can I know if an object HAS A particular style variable... When I write:

MyButton.setStyle('qsfgaeWT','-33');

It won't accomplish anything, but it also doesn't error. How can I know programmatically that 'qsfgaeWT' is NOT a valid style of 'Button'??

+2  A: 

setStyle fails silently for invalid style properties. You could try checking the style property after setting it:

MyButton.setStyle('qsfgaeWT','-33');
if (MyButton.getStyle('qsfqaeWT') == "-33") {
    // Not valid
} else {
    // valid
}
Andy E
Is this really the only way?
Joshua
@Joshua: In browser style declaration objects, you can use (Javascript) `"propertyName" in CSSStyleDeclaration`. AFAIK, ActionScript only exposes the `getStyle()` method for getting a style value, so detection can't be done using the `in` operator.
Andy E
This DOESN'T Work! Calling MyButton.setStyle('qsfgaeWT','-33'), '-33' will be returned if getStyle is then called!
Joshua
@Joshua: Hmm, I'll look into it some more when I get time later.
Andy E
A: 

displayObject is a Button added to the stage.

var value:* = displayObject.getStyle("borderColor");
trace( StyleManager.isValidStyleValue(value).toString() );  // outputs true
value = displayObject.getStyle("qwerty");
trace( StyleManager.isValidStyleValue(value).toString() );  // outputs false
value = displayObject.getStyle("color");
trace( StyleManager.isValidStyleValue(value).toString() );  // outputs true
daniel.reicher
`StyleManager.isValidStyleValue` If you pass the value returned by a `getStyle()` method call to this method, it returns `true` if the style was set and `false` if it was not set. It doesn't say anything about the style being blah-blah http://livedocs.adobe.com/flex/3/langref/mx/styles/StyleManager.html#isValidStyleValue%28%29
Amarghosh