The Space For App Developers

Beyond Plain Old HTML Objects

Extending model objects with ObjectProxy class

with 5 comments

Recently I was looking for an easy way to extend my existing model objects with additional properties. The problem I had was that there was no option to mark these model classes as dynamic. What I came up with was a solution that uses the ObjectProxy class that comes with Flex. ObjectProxy essentially is a class that allows you to track property changes of its wrapped object. So what I also got with this approach was an ability to register a PropertyChangeEvent handler with my model objects.

The snippet below shows how I extend my User class object with an additional selected property. This is a view specific property so it wouldn’t really be a good practice to do it directly in the model.

	// Wrapping each user into ObjectProxy with selected additional property selected 
	var userProxy:ObjectProxy = new ObjectProxy({user:user, selected:false});
	// Registering event PROPERTY_CHANGE handle
	userProxy.addEventListener(PropertyChangeEvent.PROPERTY_CHANGE, 
		userProxy_propertyChangeHandler);

Below is a reason why I needed all that and how it works in practice; you can right-click the example to view the source code. BTW: If you are looking for a simple and quick way to have a dynamic Checkbox List this a way to go.


This movie requires Flash Player 10

Another thing that the ObjectProxy class can resolve is a very common warning that occurs when you try to bind to not bindable properties: warning: unable to bind to property 'foo' on class 'Object' (class is not an IEventDispatcher).

Written by Piotr Walczyszyn

November 8th, 2010 at 4:20 pm

Posted in Examples

Tagged with

5 Responses to 'Extending model objects with ObjectProxy class'

Subscribe to comments with RSS or TrackBack to 'Extending model objects with ObjectProxy class'.

  1. I found this very useful too.

    Paras Sheth

    8 Nov 10 at 4:49 pm

  2. Good example of using Object Proxy. If you are looking for simliar implementations, take a look at this blog post by Andrea. http://www.devahead.com/blog/2009/12/dynamic-objectproxy/

    Dan

    9 Nov 10 at 6:42 am

  3. [...] Just highlighting some points: – Your view never can know your services you always have to link them into map file. – You dont need any code in your view (action script) because you can use the mediator to deal with all the callback functions as well as to populate your view. -I`m proxing the object user, for more information goto here [...]

  4. This excellent post helped me very much! Bookmarked your site, very interesting categories everywhere that I see here! I appreciate the info, thanks.

    Marni Vollick

    14 Feb 11 at 11:09 pm

  5. Iíd must verify with you here. Which is not something I normally do! I enjoy studying a put up that may make folks think. Additionally, thanks for allowing me to remark!

    Ernst Gomez

    14 May 11 at 6:57 pm

Leave a Reply