<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Posts about Actionscript</title>
    <link>http://wiki.futuretoby.com/tag/actionscript</link>
    <language>en-us</language>
    <item>
      <title>Vector for ActionScript3</title>
      <description>I just&amp;nbsp;&lt;a href="http://stackoverflow.com/questions/1510231/how-to-use-vector-in-flex"&gt;learned about&lt;/a&gt;&amp;nbsp;&lt;a href="http://livedocs.adobe.com/flex/3/langref/Vector.html"&gt;Vector&lt;/a&gt;, it's a typed Array for Actionscript 3, like generics in Java or C#. However, in Actionscript 3, Vector is a one-off: it's the only class that uses the generics syntax, and developers cannot use generics in their own classes. Nevertheless, Vector gives a type-safe alternative to Array and promises big performance gains. Given that it can also&amp;nbsp;&lt;a href="http://stackoverflow.com/questions/1510231/how-to-use-vector-in-flex/1510568#1510568"&gt;give the type&lt;/a&gt;&amp;nbsp;of the containing elements at runtime, I can use it now to drive JSON deserialization into objects too.</description>
      <pubDate>Fri, 02 Oct 2009 12:28:52 -0500</pubDate>
      <guid>http://wiki.futuretoby.com/Vector_for_ActionScript3</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/Vector_for_ActionScript3</link>
    </item>
    <item>
      <title>Modifying Core Types in ActionScript 3 Using the Prototype Object</title>
      <description>ActionScript 3 has a Javascript lineage. It was essentially a fork of Ecmascript. Plus, the Adobe team has worked hard on making the language a proper superset of the Ecmascript specification. This is why although ActionScript 3 was a major architechtural change from 2, and as a result became much more like Java than Javascript, it still has support for the&amp;nbsp;&lt;a href="Dissecting the Prototype Chaining"&gt;prototype object&lt;/a&gt;.&amp;nbsp;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;When I learned about the prototype object, it was a fresh air from traditional class-based OOP. Studying cool libraries like&amp;nbsp;&lt;a href="http://prototypejs.org"&gt;prototype.js&lt;/a&gt;&amp;nbsp;made me realize that the prototype model makes javascript far more flexible than some other strictly class-based OO languages.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;One of the cool things you can do with the prototype object in javascript is modify the core classes of the language, like &lt;span class="Apple-style-span" style="font-style: italic;"&gt;Array, String, &lt;/span&gt;and &lt;span class="Apple-style-span" style="font-style: italic;"&gt;Date&lt;/span&gt;. You can do this in ActionScript 3 too, for it conforms to Ecmascript 4. For example, let's say you want to write the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;collect&lt;/span&gt;&amp;nbsp;function for arrays &lt;span class="Apple-style-span" style="font-style: italic;"&gt;a la&lt;/span&gt; ruby. You would do:&lt;/div&gt;&lt;pre&gt;Array.prototype.collect = function(f){&lt;/pre&gt;&lt;pre&gt;  var ret = [];&lt;/pre&gt;&lt;pre&gt;  for each(var it in this) ret.push(f(it));&lt;/pre&gt;&lt;pre&gt;  return ret;&lt;/pre&gt;&lt;pre&gt;}&lt;/pre&gt;&lt;div&gt;There you see me using the nice &amp;nbsp;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;for each&lt;/span&gt;&amp;nbsp;syntax. But, this is going to break the behavior of the array, because now the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;collect&lt;/span&gt;&amp;nbsp;function is going to show up in the enumeration in the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;for each&lt;/span&gt;&amp;nbsp;loops. We don't want that, so to fix that we are going to set the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;collect&lt;/span&gt;&amp;nbsp;attribute of &lt;span class="Apple-style-span" style="font-style: italic;"&gt;Array.prototype&lt;/span&gt; to not be enumerable:&lt;/div&gt;&lt;pre&gt;Array.prototype.setPropertyIsEnumerable('collect', false);&lt;/pre&gt;&lt;div&gt;This allows you to write the following code:&lt;/div&gt;&lt;pre&gt;[1,2,3].collect(function(i){ return i * 2; });&lt;/pre&gt;&lt;pre&gt;// result would be [2,4,6]&lt;/pre&gt;&lt;div&gt;It's kinda tedious to have to &lt;span class="Apple-style-span" style="font-style: italic;"&gt;setPropertyIsEnumerable&amp;nbsp;&lt;/span&gt;for every time we add a function to an existing type, so I wrote a convience function:&lt;br&gt;&lt;/div&gt;&lt;pre&gt;function addMethodsTo(cls:Class, methods){
    for (var name:String in methods){
        cls.prototype[name] = methods[name];
        cls.prototype.setPropertyIsEnumerable(name, false);
    }
}&lt;br&gt;&lt;/pre&gt;&lt;div&gt;Which you can use like so:&lt;/div&gt;&lt;pre&gt;addMethodsTo(Array, {
    collect: function(f){
        var ret = [];
        for each(var it in this) ret.push(f(it));
        return ret;
    },&lt;/pre&gt;&lt;pre&gt;    anotherMethod: function(){&lt;/pre&gt;&lt;pre&gt;        ...&lt;/pre&gt;&lt;pre&gt;    },&lt;/pre&gt;&lt;pre&gt;    ...
});&lt;br&gt;&lt;/pre&gt;&lt;div&gt;This is sort of like the style prototype.js uses for extending/creating classes.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;h2&gt;A Word of Caution&lt;br&gt;&lt;/h2&gt;&lt;div&gt;Before you consider going further with this, I must advice you to think twice before using this technique(however, I hope you &lt;span class="Apple-style-span" style="font-style: italic;"&gt;do&lt;/span&gt; decide to use it afterwards ;). There are several caveats:&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;The prototype-based style is a second-class citizen in the world of ActionScript 3 and Flex. The Adobe team as well as the community seem to be much more committed to the class-based approach. I will describe some of the rough edges below.&lt;/li&gt;&lt;li&gt;You will give up compile time type checking for the portions of your code that use this style.&lt;/li&gt;&lt;li&gt;Prototype inheritence is handled by a completely different mechanism than class-based inheritence in the Flash VM and is not as performant.&lt;/li&gt;&lt;/ol&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;h2&gt;Where to put this Code?&lt;br&gt;&lt;/h2&gt;&lt;div&gt;You saw the code example above, but, where do you put it? Since I decided to use a helper function(&lt;span class="Apple-style-span" style="font-style: italic;"&gt;addMethodsTo)&lt;/span&gt;, the code cannot be directly pasted inside the class scope of a class unless the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;addMethodsTo&lt;/span&gt;&amp;nbsp;function is declared to be static(you can only make a method call directly inside class scope if it is a class method). As, a general solution, I'd rather the code be portable. So it should be includable in both class and function scope, and also, I'd like it not to pollute any namespaces.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;My current solution is to put this bit of code inside an anonymous function which immediately gets executed:&lt;/div&gt;&lt;pre&gt;// contents of includes/Array.as&lt;/pre&gt;&lt;pre&gt;(function(){
    include 'addMethodsTo.as';
    addMethodsTo(Array, {
        collect: function(f){
            var ret = [];
            for each(var it in this) ret.push(f(it));
            return ret;
        }
    });
})();&lt;br&gt;&lt;/pre&gt;&lt;div&gt;The &lt;span class="Apple-style-span" style="font-style: italic;"&gt;addMethodTo &lt;/span&gt;function&amp;nbsp;is pulled into a separate file to be easily includable else where.&lt;/div&gt;&lt;div&gt;So, with this, I would do the following to include this Array functionality:&lt;/div&gt;&lt;pre&gt;include 'includes/Array.as';&lt;/pre&gt;&lt;div&gt;Head over to&amp;nbsp;&lt;a href="http://github.com/airportyh/misc/tree/aff1496c9f99ff890801e5ecacdefdbf78be1016/prototype_in_as"&gt;Github&lt;/a&gt;&amp;nbsp;for the complete structure of the files.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;These methods are added during runtime - at exactly the time the above line of included code is executed, and not a moment before. I like to do the include at the top level of the application, this way the entire program has immediate access to the new methods. Oh, and when I said &lt;span class="Apple-style-span" style="font-style: italic;"&gt;entire program&lt;/span&gt;, I do mean the entire program - not just the files that happen to include the file. Well, this is good &lt;span class="Apple-style-span" style="font-style: italic;"&gt;and&lt;/span&gt;&amp;nbsp;bad. This means if you create components that use the array extensions you've created without explicitly including it(you've included it at the entry point of your program), then you have created an &lt;span class="Apple-style-span" style="font-style: italic;"&gt;invisible dependency&lt;/span&gt;. If you try to take the component and use it in a different project without the array extensions, it will not work. Of course, you could also make the dependency explicit by including the file everywhere you are using them, but 1) that's kinda tedious/repetitious, and 2) there's nothing enforcing you to do this.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;h2&gt;Pitfalls and Gotchas&lt;br&gt;&lt;/h2&gt;&lt;div&gt;As I mentioned, the prototype-based programming style is a second-class citizen in the ActionScript 3 world, and so, its use is not particularly well supported. First of all, the compiler does not recognize any of the methods added via the prototype mechanism, and thus cannot perform static type checking on them. But what is funny is the way the compiler copes with this - it depends...on the type in question. For Arrays, the compiler simply allows all method calls - you can call any method on an array, even if it doesn't exist, the compiler won't complain. So, for a call like:&lt;/div&gt;&lt;pre&gt;[1, 2, 3].collect(...);&lt;/pre&gt;&lt;div&gt;The compiler won't even say a word. But for Dates, it gives a warning message. This:&lt;/div&gt;&lt;pre&gt;new Date().format()&lt;br&gt;&lt;/pre&gt;&lt;div&gt;would trigger this warning:&lt;/div&gt;&lt;pre&gt;Warning: format is not a recognized method of the dynamic class Date.&lt;br&gt;&lt;/pre&gt;&lt;div&gt;But, for strings, it's a different story still:&lt;/div&gt;&lt;pre&gt;'one, two, three'.csv2Array()&lt;br&gt;&lt;/pre&gt;&lt;div&gt;compiler says:&lt;/div&gt;&lt;pre&gt;Error: Call to a possibly undefined method csv2Array through a&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;    reference with static type String.&lt;br&gt;&lt;/pre&gt;&lt;div&gt;And the same thing with numbers:&lt;/div&gt;&lt;pre&gt;(2).minutes().ago()&lt;br&gt;&lt;/pre&gt;&lt;div&gt;compiler says:&lt;/div&gt;&lt;pre&gt;Error: Call to a possibly undefined method minutes through a&amp;nbsp;&lt;/pre&gt;&lt;pre&gt;    reference with static type int.&lt;br&gt;&lt;/pre&gt;&lt;div&gt;The work around for strings and numbers is to upcast it to an Object:&lt;/div&gt;&lt;pre&gt;Object('one, two, three').csv2Array();&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;Object(2).minutes().ago();&lt;br&gt;&lt;/pre&gt;&lt;div&gt;or if you just have an untyped variable, it'll work just fine:&lt;/div&gt;&lt;pre&gt;var x = 2;&lt;/pre&gt;&lt;pre&gt;x.minutes().ago();&lt;/pre&gt;&lt;div&gt;See the full code example&amp;nbsp;&lt;a href="http://github.com/airportyh/misc/blob/366479f05b6ef862bd48a83654b511fcd18b3b20/prototype_in_as/ex1.mxml"&gt;here&lt;/a&gt;, and the&amp;nbsp;&lt;a href="http://tobyho.com/uploads/flex_examples/prototype/ex1.swf"&gt;demo&lt;/a&gt;&amp;nbsp;here.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;What about runtime error handling? So let's see what happens if you don't include the extensions and run the code. When I took out the array extension methods, the runtime dutifully throw me the:&lt;/div&gt;&lt;pre&gt;TypeError: Error #1006: collect is not a function.&lt;br&gt;&lt;/pre&gt;&lt;div&gt;This is good, just what I would expect. For Date, it works the same way. Now let's try taking out the string extensions:&lt;/div&gt;&lt;pre&gt;TypeError: Error #1006: value is not a function.&lt;br&gt;&lt;/pre&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;Uhh, what? Not really sure what you mean.&lt;/span&gt; And as you would expect, it works like this for Number&amp;nbsp;as well.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;I have also seen cases where the runtime simply completely &lt;span class="Apple-style-span" style="font-style: italic;"&gt;muffles&lt;/span&gt; an error when there's an undefined method being tried as someone documented&amp;nbsp;&lt;a href="http://turbidwater.blogspot.com/2007/12/flex-sucks-ass.html"&gt;here&lt;/a&gt;. But I am not able to reproduce this just now. Also on another note, the error stacktrace from the flash player(debug version) is not very helpful because although it gives the call stack, there are no line numbers. I am sure that you'd get a better experience using Flex Builder, however.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;I think that about wraps it up. Although extending core types is fun, powerful, and elegent, it's also full of holes and flying scissors everywhere. Are &lt;span class="Apple-style-span" style="font-style: italic;"&gt;you&lt;/span&gt;&amp;nbsp;ready to jump into this brave new world?&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;A message to the Adobe Flex/ActionScript team: I implore you to put more effort into the prototype-based side of the language. It allows for many possibilities which its class-based counterpart cannot offer. I don't dislike the class-based approach. I think the two each have their own strengths and weakness. Which is why I love this hybrid aspect of ActionScript 3 which allows me to use either style in the same environment. I believe that if the prototype-based aspect of ActionScript were to improve and get more exposure, it would not only become a better language, but also a more wide spread language.&lt;/span&gt;&lt;/div&gt;&lt;/div&gt;&lt;div&gt;&lt;span class="Apple-style-span" style="font-style: italic;"&gt;&lt;br&gt;&lt;/span&gt;&lt;/div&gt;&lt;h2&gt;Other Flex-related Articles on my Site&lt;/h2&gt;&lt;div&gt;&lt;ul&gt;&lt;li&gt;&lt;a href="http://tobyho.com/Making_Flex_Suck_Less"&gt;Making Flex Suck Less&lt;/a&gt;&lt;br&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://tobyho.com/Property_Binding_in_Flex"&gt;Property Binding in Flex&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a href="http://tobyho.com/Adding_Real_Properties_to_FABridge"&gt;Adding Real Properties to the FABridge&lt;/a&gt;&lt;/li&gt;&lt;/ul&gt;&lt;/div&gt;</description>
      <pubDate>Sat, 02 May 2009 21:41:27 -0500</pubDate>
      <guid>http://wiki.futuretoby.com/Modifying_Core_Types_in_ActionScript_3_Using_the_Prototype_Object</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/Modifying_Core_Types_in_ActionScript_3_Using_the_Prototype_Object</link>
    </item>
    <item>
      <title>Different Ways of Including Functions in ActionScript</title>
      <description>This is another example of something that would have been a blog post&#160;&lt;a href="http://stackoverflow.com/questions/775093/one-file-per-function-really/782330"&gt;creeping over to SO&lt;/a&gt;.</description>
      <pubDate>Thu, 23 Apr 2009 11:51:41 -0500</pubDate>
      <guid>http://wiki.futuretoby.com/Different_Ways_of_Including_Functions_in_ActionScript</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/Different_Ways_of_Including_Functions_in_ActionScript</link>
    </item>
    <item>
      <title>Property Binding in Flex</title>
      <description>I have been working more with Flex now, and I have to say: the data binding features(covered&#160;&lt;a href="http://livedocs.adobe.com/flex/3/html/help.html?content=databinding_1.html"&gt;here&lt;/a&gt;) in Flex is beautiful. It's light years ahead of anything I've ever seen before.&#160;&lt;div&gt;&lt;br&gt;&lt;div&gt;Example 1: You want a Text element to show the full name of current selected state from a drop down:&#160;&lt;pre&gt;    &amp;lt;List id="stateList" labelField="abrv"&gt;
        &amp;lt;ArrayCollection&gt;
            &amp;lt;Object abrv="NJ" full="New Jersey"/&gt;
            &amp;lt;Object abrv="MA" full="Massachusetts"/&gt;
            &amp;lt;Object abrv="NY" full="New York"/&gt;
            &amp;lt;Object abrv="NC" full="North Carolina"/&gt;
        &amp;lt;/ArrayCollection&gt;
    &amp;lt;/List&gt;
    &amp;lt;Text text="{stateList.selectedItem.full}"/&gt;&lt;br&gt;&lt;/pre&gt;&lt;div&gt;The code in the curly braces is some ActionScript code, but it doesn't just get executed once, oh no, you can look at it as an invariant, i.e. the content of the said text element will &lt;span class="Apple-style-span" style="font-style: italic;"&gt;always &lt;/span&gt;contain the value expressed by &lt;span class="Apple-style-span" style="font-style: italic;"&gt;stateList.selectedItem.full. &lt;/span&gt;Therefore, when you change the selection of &lt;span class="Apple-style-span" style="font-style: italic;"&gt;stateList&lt;/span&gt;, the text element will immediately change with it. Here's the&#160;&lt;a href="http://github.com/airportyh/misc/blob/ed1d453849a6a876aa8763357016201b06dcb98d/flex_binding/ex1.mxml"&gt;code&lt;/a&gt;&#160;and&#160;&lt;a href="http://tobyho.com/uploads/flex_examples/ex1.swf"&gt;live&#160;demo&lt;/a&gt;&#160;for example 1.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;div&gt;Example 2: Let's try binding to a custom variable rather than to another control. We first make our &lt;span class="Apple-style-span" style="font-style: italic;"&gt;counter&lt;/span&gt; variable public and bindable:&lt;/div&gt;&lt;pre&gt;    &amp;lt;Script&gt;
    [Bindable]
    public var counter:int = 0;&lt;br&gt;&lt;/pre&gt;&lt;div&gt;Then, we can bind a text control to it:&lt;/div&gt;&lt;pre&gt;    &amp;lt;Text text="{counter}"/&gt;&lt;br&gt;&lt;/pre&gt;&lt;div&gt;Next, we can periodically increment the variable like so:&lt;/div&gt;&lt;pre&gt;        setInterval(function(){
            counter++;
        }, 1000);&lt;br&gt;&lt;/pre&gt;&lt;div&gt;And you will see the number in the text display count up: the display in the text element is syncronized with the actually value of &lt;span class="Apple-style-span" style="font-style: italic;"&gt;counter&lt;/span&gt;.&#160;The&#160;&lt;a href="http://github.com/airportyh/misc/blob/ed1d453849a6a876aa8763357016201b06dcb98d/flex_binding/ex2.mxml" style="color: rgb(136, 136, 136); "&gt;code&lt;/a&gt;&#160;and&#160;&lt;a href="http://tobyho.com/uploads/flex_examples/ex2.swf" style="color: rgb(136, 136, 136); "&gt;live demo&#160;&lt;/a&gt;for example 2.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;Next, let's try creating our own properties. ActionScript has support for Java-style OOP(as supposed to Javascript style), but, &lt;span class="Apple-style-span" style="font-style: italic;"&gt;more &lt;/span&gt;interestingly, it's got direct support for properties. Add direct support of property change event bindings, and it's a beautiful thing.&#160;&lt;/div&gt;&lt;div&gt;Example 3: You have a Page object that manages the details of paginating a set of data, and you need some controls to navigate between the pages as well as display what page you are on. You create a Page object:&lt;/div&gt;&lt;pre&gt;    class Page extends EventDispatcher{
        public var number:int;
        public var size:int;
        public var total:int;&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;        ...&lt;/pre&gt;&lt;pre&gt;    }&lt;/pre&gt;&lt;div&gt;A page has a page number, a page size, and the total size of the data set(used to tell whether a page exists). I instantiate a Page object:&lt;/div&gt;&lt;pre&gt;    &amp;lt;Script&gt;
    [Bindable]
    public var currentPage:Page = new Page(0, 10, 100);
    &amp;lt;/Script&gt;&lt;br&gt;&lt;/pre&gt;&lt;div&gt;I need a label that tells me the range of results currently being viewed:&lt;/div&gt;&lt;pre&gt;    &amp;lt;Label text="{currentPage.from} to {currentPage.to}"/&gt;&lt;br&gt;&lt;/pre&gt;&lt;div&gt;Here, the label binds to 2 properties: &lt;span class="Apple-style-span" style="font-style: italic;"&gt;from&lt;/span&gt;&#160;and &lt;span class="Apple-style-span" style="font-style: italic;"&gt;to&lt;/span&gt;. Let's implement these properties:&lt;/div&gt;&lt;pre&gt;    class Page extends EventDispatcher{&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;        ...&lt;/pre&gt;&lt;pre&gt;        [Bindable]
        public function get from():int{
            return number * size + 1;
        }
        [Bindable]
        public function get to():int{
            return (number + 1) * size;
        }&lt;br&gt;&lt;/pre&gt;&lt;pre&gt;        ...&lt;/pre&gt;&lt;pre&gt;    }&lt;/pre&gt;&lt;div&gt;Notice the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;get&lt;/span&gt;&#160;syntax: this in ActionScript turns the function into a getter for a property of the same name. Also notice that I made these properties bindable.&lt;/div&gt;&lt;div&gt;Next, I need the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;previous&lt;/span&gt;&#160;and &lt;span class="Apple-style-span" style="font-style: italic;"&gt;next&lt;/span&gt;&#160;buttons. I choose to sandwich the label in between them:&lt;/div&gt;&lt;pre&gt;    &amp;lt;Button label="Previous" click="currentPage.prev()"/&gt;
    &amp;lt;Label text="{currentPage.from} to {currentPage.to}"/&gt;
    &amp;lt;Button label="Next" click="currentPage.next()"/&gt;&lt;br&gt;&lt;/pre&gt;&lt;div&gt;Clicking on P&lt;span class="Apple-style-span" style="font-style: italic;"&gt;revious&lt;/span&gt;&#160;will call the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;prev &lt;/span&gt;method and clicking on &lt;span class="Apple-style-span" style="font-style: italic;"&gt;Next&lt;/span&gt;&#160;will call the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;next&lt;/span&gt;&#160;method. Simple. Here is the code:&lt;/div&gt;&lt;pre&gt;        public function next(){
            number++;
        }
        
        public function prev(){
            number--;
        }&lt;br&gt;&lt;/pre&gt;&lt;div&gt;Compiled it, but it doesn't work. Clicking on &lt;span class="Apple-style-span" style="font-style: italic;"&gt;Previous&lt;/span&gt;&#160;and &lt;span class="Apple-style-span" style="font-style: italic;"&gt;Next&lt;/span&gt;&#160;does no change the display on the label at all. Oh! We need to fire the events! To fire a property change event, you'd do something like:&lt;/div&gt;&lt;pre&gt;                this.dispatchEvent(new PropertyChangeEvent(
                    "propertyChange", true, true, 
                    PropertyChangeEventKind.UPDATE,
                    propertyName, null, null, this));&lt;br&gt;&lt;/pre&gt;&lt;div&gt;In our case, we are going to fire the change events for both &lt;span class="Apple-style-span" style="font-style: italic;"&gt;from &lt;/span&gt;and &lt;span class="Apple-style-span" style="font-style: italic;"&gt;to&lt;/span&gt;, because both of those must change after you flip a page. So we fire the changes:&lt;/div&gt;&lt;pre&gt;        private function fireChanges(){
            var toFire = ['from', 'to'];
            for (var i = 0; i &amp;lt; toFire.length; i++)
                this.dispatchEvent(new PropertyChangeEvent(
                    "propertyChange", true, true, 
                    PropertyChangeEventKind.UPDATE,
                    toFire[i], null, null, this));
        }
        public function next(){
            number++;
            fireChanges();
        }
        public function prev(){
            number--;
            fireChanges();
        }&lt;br&gt;&lt;/pre&gt;&lt;div&gt;Ok, but I would like to disable the &lt;span class="Apple-style-span" style="font-style: italic;"&gt;Previous&lt;/span&gt;&#160;or &lt;span class="Apple-style-span" style="font-style: italic;"&gt;Next&lt;/span&gt;&#160;button when we are at the begining or the end of the dataset. No problem! We create a couple more bindable properties: &lt;span class="Apple-style-span" style="font-style: italic;"&gt;hasNext&lt;/span&gt;&#160;and &lt;span class="Apple-style-span" style="font-style: italic;"&gt;hasPrev&lt;/span&gt;:&lt;/div&gt;&lt;pre&gt;        [Bindable]
        public function get hasNext():Boolean{
            return to &amp;lt; total;
        }
        [Bindable]
        public function get hasPrev():Boolean{
            return number &gt; 0;
        }&lt;br&gt;&lt;/pre&gt;&lt;div&gt;Bind the buttons' &lt;span class="Apple-style-span" style="font-style: italic;"&gt;enabled&lt;/span&gt;&#160;property to them:&lt;/div&gt;&lt;pre&gt;    &amp;lt;Button label="Previous" click="currentPage.prev()"&#160;&lt;/pre&gt;&lt;pre&gt;        enabled="{currentPage.hasPrev}"/&gt;
    &amp;lt;Label text="{currentPage.from} to {currentPage.to}"/&gt;
    &amp;lt;Button label="Next" click="currentPage.next()"&lt;/pre&gt;&lt;pre&gt;        enabled="{currentPage.hasNext}"/&gt;&lt;br&gt;&lt;/pre&gt;&lt;div&gt;Make sure to fire the change events for them too when you are flipping pages(add them to &lt;span class="Apple-style-span" style="font-style: italic;"&gt;toFire &lt;/span&gt;in the implementation of &lt;span class="Apple-style-span" style="font-style: italic;"&gt;fireChanges&lt;/span&gt;)&#160;and we are done.&#160;The code:&#160;&lt;a href="http://github.com/airportyh/misc/blob/ed1d453849a6a876aa8763357016201b06dcb98d/flex_binding/ex3.mxml" style="color: rgb(136, 136, 136); "&gt;ex3.mxml&lt;/a&gt;,&#160;&lt;a href="http://github.com/airportyh/misc/blob/ed1d453849a6a876aa8763357016201b06dcb98d/flex_binding/Page.as" style="color: rgb(136, 136, 136); "&gt;Page.as&lt;/a&gt;&#160;and&#160;&lt;a href="http://tobyho.com/uploads/flex_examples/ex3.swf" style="color: rgb(136, 136, 136); "&gt;live demo&lt;/a&gt;&#160;for example 3.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;The end result of this is that you have very well separated MVC code. There isn't any code whose sole purpose is to sync the display like you get with jQuery code, for example. The code to do this is very minimal and naturally readable.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;All code examples can be found&#160;&lt;a href="http://github.com/airportyh/misc/tree/ed1d453849a6a876aa8763357016201b06dcb98d/flex_binding"&gt;here&lt;/a&gt;&#160;on Github.&lt;/div&gt;&lt;div&gt;&lt;br&gt;&lt;/div&gt;&lt;div&gt;Btw, I should mention that, the code inside of the curly braces does not allow any arbitrary ActionScript. There is some amount of flexibility, but not a lot. You can read more&#160;&lt;a href="http://livedocs.adobe.com/flex/3/html/help.html?content=databinding_2.html"&gt;here&lt;/a&gt;.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</description>
      <pubDate>Wed, 11 Mar 2009 01:01:36 -0500</pubDate>
      <guid>http://wiki.futuretoby.com/Property_Binding_in_Flex</guid>
      <author>toby ho</author>
      <link>http://wiki.futuretoby.com/Property_Binding_in_Flex</link>
    </item>
  </channel>
</rss>

