Flex Examples
Detecting changes to an Object using the Flex ObjectProxy class
The following example shows how you can detect changes to an Object object by listening for the propertyChange event on the ObjectProxy instance.
Full code after the jump.
<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2007/09/27/detecting-changes-to-an-object-using-the-flex-objectproxy-class/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="vertical"
verticalAlign="middle"
backgroundColor="white"
creationComplete="init();">
<mx:Script>
<![CDATA[
import mx.events.PropertyChangeEvent;
import mx.utils.ObjectProxy;
private var object:Object = {};
private var objectProxy:ObjectProxy;
private function init():void {
objectProxy = new ObjectProxy(object);
objectProxy.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, updateChange);
objectProxy.name = "My Object";
objectProxy.id = 31;
/* Note: Any assignments made directly to the "object"
Object do not dispatch the propertyChange event. */
object.isDebug = false;
object.id = 33;
/* Note: Even though the earlier assignment to the "id"
property didn't dispatch the propertyChange event,
the "oldValue" property is still displayed as 33. */
objectProxy.id = 45;
}
private function updateChange(evt:PropertyChangeEvent):void {
arrColl.addItem(evt);
}
]]>
</mx:Script>
<mx:ArrayCollection id="arrColl" />
<mx:DataGrid dataProvider="{arrColl}"
sortableColumns="false"
draggableColumns="false"
width="100%"
height="100%">
<mx:columns>
<mx:DataGridColumn dataField="type" />
<mx:DataGridColumn dataField="property" />
<mx:DataGridColumn dataField="newValue" />
<mx:DataGridColumn dataField="oldValue" />
<mx:DataGridColumn dataField="source" />
</mx:columns>
</mx:DataGrid>
</mx:Application>
View source is enabled in the following example.
Tagged with: propertyChange
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
0 Responses to Detecting changes to an Object using the Flex ObjectProxy class
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


Sweet! Will this work for custom classes (like below) as well?
package Classes{ [Bindable] public class Users{ public var UserBadgeID:String = ""; public var UserEmail:String = ""; public function Users(){ } } }Man, this rocks…didn’t know about that objectProxy….very tight!!!
I copied and pasted the above code in a new flex application and i get the following error:
1137: Incorrect number of arguments. Expected no more than 0.
Even i search in help about the constructor of ObjectProxy class and this it should working, it doesn’t leave to put any arguments in new ObjectProxy object creation.
I ‘m using Flex 2.0.1
Antonis,
Sorry, I think I only tested using the Flex 3 Beta.
Peter
I also download and install flex builder 3 (Flex 3 M3 (Beta 2)) and same error occured.
1137: Incorrect number of arguments. Expected no more than 0.
Antonis,
I just double checked with Beta 2 and didn’t see any errors at compile-time or run-time.
Are you sure you’re publishing with the beta 2 SDK and not an earlier version? Also, which version of Flash Player are you using? (if you aren’t sure, I recently added an “About you” page to this site at http://blog.flexexamples.com/about-you/).
Peter
I finnally found the cause of the error.
I had created a new project with name ObjectProxy and the applacation name was ObjectProxy too.
So there was a name conflict when the ObjectProxy object was created.
Thanks for your time.
Actually I think you don’t have to use ObjectProxy for that.
Try this:
public class Test { [Bindable] public var test:Boolean; }—
var t:Test = new Test(); t.addEventListener("propertyChange", Handler.onPropertyChange); t.test = true;You will get and event also… even if your object doesn’t extend event dispatcher. I’ve tested this code Flex Builder 3 Beta 2.
The above works on Flex Builder 2 also. You don’t need to use ObjectProxy. In JavaScript 1.5 the equivalent is Object.watch().
Hello,
I have coupled this method with an editable datagrid to submit each change, and no problem, it rock’s !
But now I have another datagrid with a combobox cellRenderer column (like in this example : http://philflash.inway.fr/flex/dgRendererSimple/srcview/index.html) but the cellRenderer doesn’t fire the PropertyChangeEvent… maybe the affectation is made programmatically ?? I have tried to simulate the event (in the onChange method of the cellRenderer) but no way… any idea to solve that ?
Thanks in advance for any suggestion