This specific problem has been annoying me for a while. I personally don’t like using Repeaters, they aren’t the best way to handle the repetition of a specific element and also, they can cause memory leaks if not used properly. When I do use them, it’s because I’m creating a prototype or in a time crunch.
I use the ‘this’ keyword a good amount in my code. I like it because it differentiates in a quick and effective matter that the current variable being referenced is a class variable and not a function variable. However, if you have the following:
<mx:Repeater id="rp" dataProvider="this._repeaterData" >
<custom:SomeComponent dataProvider="{this._data}" />
</mx:Repeater>
In this example, we have a Repeater using ‘this._repeaterData’, that is repeating a ‘SomeComponent’ component that binds the ‘this._data’ variable. Most of you would think that this works since the variable in your class is available and that the compiler is not giving any errors. However, when you try this example, you will notice that what you set in the ‘this._data’ isn’t binding properly to the component. Why is that?
This would be the work of the Repeater. Flex doesn’t let you know about this flaw, but whatever is within the Repeater tag changes the scope of the ‘this’ keyword. In this case, the ‘this’ keyword points to the Repeater itself and no errors actually occur even though there should be one. You are left debugging this small piece of code for the longest time until you notice that removing the ‘this’ keyword within the Repeater actually fixes this problem. In fact, this bit of code actually works:
<mx:Repeater id="rp" dataProvider="this._repeaterData" >
<custom:SomeComponent dataProvider="{_data}" />
</mx:Repeater>
It works because of the way Flash looks for variables within a scope (which is another subject altogether). This is a pretty big flaw in the compiler which can cause a lot of grief and hopefully will be fixed in the future.
Copyright © Thinking in Code. All Rights Reserved. Powered by Wordpress