Binding issue, unable to bind to property * on class * - actionscript-3

I have a class named Student, it has several properties along with the property "isSelected:Boolean"and the class is defined Bindable.
[Bindable]
[RemoteClass(alias="portal::Student")]
public class Student
In an mxml application I have a datagrid in which its dataprovider has been set to an ArrayCollection of Students. I have a column of checkboxex for the datagrid along with a headerItemRenderer checkbox which is supposed to select all the students (all checkboxes in rows should be selected or deselected).
I have defined a handler for the click event of the checkbox in the header which sets the isSelected property of each Student object in dataProvider to false or true. But on click of this check box in the header, I get the warning: unable to bind to property 'isSelected' on class 'Student' and therefore check boxes in rows do not get updated.
I don't get why the binding does not work here and don't know what to do to fix this issue. Any help is greatly appreciated.

It depends, i guess you have somewhere in the code a reference to the student instance, you want to bind on. This reference has to be declared [Bindable] as well, in order to get the binding chain to work.
Also, when you not depend on ActionScript to get the binding to work, you don't have to extend EventDispatcher, because MXML will generate that code for you (with regards to Cosma's comment).
Do you define an inline renderer? This is not the optimum in my opinion, since a component is generated and debugging gets a lil fuzzy, since you might run into scope problems. I'd recommend implementing a new component, implement IDataRenderer and propagate the data to the children through the live cycle or implement the component in MXML. Why IDataRenderer? This interface is necessary.
Also, i'm not sure on that one, but shouldn't it be
[Bindable]
[RemoteClass(alias="portal.Student")]
public class Student {}
?

Let your actionscript class extends EventDispatcher (binding is based on event dispatching).

You need to make sure that your ItemRenderer saves the incoming data in a property marked as [Bindable]. You'll want to pass the entire Student in, rather than just "isSelected"...

Related

Actionscript 3: Adding a MovieClip (from library but not on stage) to a Sprite declared within a seperate class

What I am trying to do is add a MovieClip which I have created, but deleted from the actual stage itself, from a seperate class to a sprite which I declare in the document class.
The link between the main document class and the others works fine, as well as references to the stage, from the class in question.
It is only one line of the function which is giving me the error: Type was not found, or is not compile-time constant:SequenceA
I have already tried changing the settings for 'Export for Actionscript' in the library properties and still get the same message every time.
Pretty lost with this one so any help is appreciated. Cheers!
What have you set as the "Class" in your Export for ActionScript settings? It looks like you're trying to refer to an instance rather than the class itself. For example, mc is an instance of the MovieClip Class.
Assuming you've chosen the Class "SequenceA", you would do this instead:
var seqA:SequenceA = new SequenceA();
There's a little bit more detail about objects and classes here.

dynamic class in AS3 : listening to property creation?

I'm currently working on a project that involve a re-implementation of the Array class.
This object needs to be an Array for compatibility reasons, while I also need to keep control of what is written in.
I cannot seem to find any way to check property creation inside of a dynamic object in AS3. Something that would work like the Event.ADDED_TO_STAGE but, like, ClassEvent.PROPERTY_ADDED.
I override methods like push, splice etc, but I cannot control direct assignation : MyArray[i] = ...
Is such a thing even possible ?
Of course, I could make some kind of validations elsewhere, but this would involve accessing a part of the code I cannot modify.
Thanks for your time !
I'm not sure I follow you entirely but you may be looking for the Proxy class:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/utils/Proxy.html
An example at the bottom shows you how you can override direct assignment:
override flash_proxy function setProperty(name:*, value:*):void {
_item[name] = value;
}
Using this you would be able to dispatch a custom event that would be fired any time an item was added to your ProxyArray

Getting a list of the rendered items

I have my own custom component. This component extends from a basic container. I want to be able to access the itemRenderer instances that are being visualized. I know that the component mx:list has an internal getter that provides an array of Arrays containing the itemRenderer instances that render each data provider item. I want the same thing. Any idea how of how to do that?
To be more specific: I am setting the selected property of my dataProvider items to true or false. From the updateDisplayList funcion of my ItemRenderer I check for changes of the property and correct the border color of the selected ones. Unfortunately I have to force the updateDisplayList function. I already did this once on a ItemRenderer from a list. Only with the list it was practical because by making my own list I was able to get the list of items being rendered and therefore visualized (cannot be many). It was no overhead to go trough the rendered Items and updateDisplayList. But in this case I can have 100 items. Imagine checking and changing styles on so many items. Thanks
The Flex architects intentionally made this difficult to do, because they are properly encapsulating the component. In short, to even try to do this is a violation of good OOP principles.
That said, about 90% of the things you are probably trying to do can be done by manipulating the data item, and the remaining 10% can be done by using a ClassFactory for your itemRenderer that sets a custom property on your itemRenderer to a callback where you can look at the data available to the containing context and provide back a value based on that.
If you elaborate a bit more on your end goal, I can give you more specifics.
Edit in light of clarification:
You need to make your data object class dispatch an event when it changes (one way is to make it bindable, or just make the selected property bindable). Then, in your renderer, listen for the change event and take the appropriate action.
A second way to handle this would just be to refresh() the collection, storing the selectedItem first (if you care about that) and resetting it once the refresh has finished.
I believe you can access the itemRenderer instances through getChildAt method. Flex 3's container overrides "getChildAt", "numChildren", given that some children are logical children, while some are decorative children such as background, border and scrollbars.
Keep in mind that itemRenderer may not right away become available upon dataProvider assignment, as they may be created during the next component lifecycle. Check with the underlying container's documentation and find out which event to be listened when the renderers are surely created, so you can reliably access them.

Dynamic Tree Generation in Flex Actionscript

I am using the URL logic for creating a dynamic Tree in Flex using action script. However the output is not properly shown (Object name is shown instead of Label).
Code is available in above mentioned URL.
Please help.
Write a correct toString() implementation of your DataTreeNode, so it would have a proper display in this tree.
An example: Provided the class DataTreeNode has a data:Object field, and this object has a urlToDisplay:String property that you want displayed. Do like this:
override public function toString():String {
if (!data) return '[null]';
return data.urlToDisplay;
}
If you only rely on simple Objects or data classes, you can use the tree's labelField or labelFunction in order to read and/or format data, which is passed to the renderer. There are no new item renderer classes needed.
New renderer should be compatible with these functions!
On a site note: item renderer are not "mostly just simple MXML classes", they are component instances. It doesn't matter how there are implemented. There are best practises like avoiding data bindings in item renderers, that's why it is common to use the markup for drawing, but implementing the views behaviour according to the Flex component live cycle. you might want to read about it in the documentation, because it is a necessary read for a Flex developer.
You will have to write an ItemRenderer that tells your tree how it should display the components. ItemRenderers are mostly just simple MXML classes that access one item each and display the data in any way you want. You will have to assign the ItemRenderer to your component.
See this article:
http://help.adobe.com/en_US/flex/using/WS03d33b8076db57b9-23c04461124bbeca597-8000.html

Event once all the custom item renderers have been created/set

I'm using a custom item renderer in my datagrid, and need to make a button visible or invisible based on if there has been a negative value on any of the values in the renderer. So I'd like to set a flag to false when the first renderer is set off, turn it to true if there's any negative values, and at the end check for the value of the flag. I know I can dispatch a dataChange event for every time the data is changed in the renderer instances, but I was wondering if there is anyway I can know when all of them are done?
Thanks!
There is no such event.
Like any other Flex component, a renderer will dispatch a CREATION_COMPLETE after it's been created. ItemRenderers are generally recycled (the same object gets assigned new data to render), thus listening for CREATION_COMPLETE is not sufficient, unless you disable the recycling.
For a Spark List component, you can disable recycling by setting useVirtualLayout=false on the layout class. I'm not sure if the Spark DataGrid class support this or not. The MX DataGrid may have some other way to do this.
Disabling the recycling, however, can have performance implications. I think your idea w/the DATA_CHANGE event is the best solution:
determine the initial state of the data (ie: are there any negative values)
in the renderer, use the DATA_CHANGE event (or just override the setter for the renderer's data property) to know when the data has changed
When the data changes, dispatch a custom event class that will bubble. This event has a property that tells you if the value is negative or not.
Since your custom event from the renderers will bubble up to the grid, you can add one listener on the grid to handle changes from all the renderers.
You should have a look into RendererExistenceEvents. You should be able to tell when they are all created based on how many items you have in your list or at least how many should be in view at once.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/spark/events/RendererExistenceEvent.html