1

I'm hoping someone can explain what is going on under the hood when using MXML curly bindings.

For example, with itemrenderers:

If I bind some control via MXML to the data source such as:

text={data.myText}

Somehow these bindings seem to get automatically cleaned up.

However, if I bind using Actionscript when am I supposed to call unwatch()? How do I know when the itemRenderer is no longer being used?

How do the MXML bindings know when to un-bind?

Someone Else
  • 2,625
  • 3
  • 20
  • 15

1 Answers1

0

With actionscript, you need to keep an instance of the watcher and clean it yourself. The curly braces is essentially a 'shortcut' that creates a lot of extra code to handle binding and clean up after itself (plus making sure it's not in a dependency loop). It's made for convenience while the actionscript version gives you more control, but creates more visible code.

I could go on, but Michael Labriola already has a great talk about the subject.

J_A_X
  • 12,857
  • 1
  • 25
  • 31
  • I'm aware i need to clean up myself, and that code is generated by the MXML bindings. The reason for the question is because it seems under some circumstances there is no way to tell when to do clean up (such as inside an item renderer), but somehow the MXML bindings know. – Someone Else May 18 '11 at 21:59
  • Ah, your question was vague. Well, first off, item renderers are recycled themselves, so it saves a lot on 'clean up' time there, but mostly it's because of the weak reference's flag. That let's the GC know that if the item renderer isn't being used anymore, it can still dispose of it because it has no hard link to the data. You can do the same with actionscript, but try to avoid it if possible (like use a IDisposable interface to clean up). – J_A_X May 18 '11 at 22:14
  • So MXML bindings use weak references? Esentially to produce the equivilent of an MXML binding is to add a weak event listener for propertyChange event? – Someone Else May 18 '11 at 22:19
  • Tell you the truth, I've never looked at all the code that gets generated and wrapped. It could be that it doesn't use weak references and just listen's for the components addToStage/removeFromStage events to know when to clean up. Look it up. – J_A_X May 18 '11 at 22:24
  • Thanks for the info! Reason I'm even wondering is due to having some complex renderers that I dont want re-running the set data() method. Trying to update data in the datasource: dataProv[2].myPerson.cars = [car1, car2, car3] and have the renderer detect the change and run the appropriate code without invalidating the list or re-setting the data. Using a weak listener will still run the code until its been GC'd, which I'm also trying to avoid. – Someone Else May 18 '11 at 22:39
  • I think you're overthinking this. The item renderer's set data will only be called when a new 'row' needs to be shown or switched. Since item renderer recycle, only the ones shown are ever called (even if you had 1000 row but you only see 10, only 10 gets 'updated). Plus, what you just described wouldn't trigger the set data again, it would rebind whatever if binding to 'cars'. – J_A_X May 19 '11 at 03:29