{"id":43,"date":"2008-10-01T09:41:44","date_gmt":"2008-10-01T16:41:44","guid":{"rendered":"http:\/\/www.nayansavla.com\/blog\/?p=43"},"modified":"2010-04-27T12:36:16","modified_gmt":"2010-04-27T19:36:16","slug":"using-changewatcher","status":"publish","type":"post","link":"https:\/\/www.nayansavla.com\/blog\/?p=43","title":{"rendered":"Using ChangeWatcher"},"content":{"rendered":"<p><!-- google_ad_section_start --><br \/>\nData binding is one of the best things about Flex, frameworks like Cairngorm rely heavily on data binding. But what if you want to detect a change in variable value and then also do something else. Changewatcher comes in handy during times like those.<br \/>\n<!-- google_ad_section_end --><br \/>\n\u00a0<\/p>\n<p>import mx.binding.utils.ChangeWatcher;<\/p>\n<p>\u00a0<\/p>\n<p>This is the class i am talking about, it lets you listen for changes in a variable. Lets say we have a class variable totalItems and we need to detect changes to that variable. This is a simple class.<\/p>\n<p>\u00a0<\/p>\n<div class=\"codecolorer-container actionscript default\" style=\"overflow:auto;white-space:nowrap;width:435px;\"><div class=\"actionscript codecolorer\">package <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n\u00a0\u00a0 \u00a0<span class=\"kw2\">class<\/span> Model <span class=\"br0\">&#123;<\/span><br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<span class=\"co1\">\/\/i shall leave the constructor to you<\/span><br \/>\n<br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<span class=\"br0\">&#91;<\/span>Bindable<span class=\"br0\">&#93;<\/span><br \/>\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0<span class=\"kw3\">public<\/span> <span class=\"kw2\">var<\/span> totalItems:<span class=\"kw3\">Number<\/span>;<br \/>\n<br \/>\n\u00a0\u00a0 \u00a0<span class=\"br0\">&#125;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>Now to detect changes to this variable, the code would look something like,<\/p>\n<div class=\"codecolorer-container actionscript default\" style=\"overflow:auto;white-space:nowrap;width:435px;height:300px;\"><div class=\"actionscript codecolorer\"><span class=\"kw3\">import<\/span> mx.<span class=\"me1\">binding<\/span>.<span class=\"me1\">utils<\/span>.<span class=\"me1\">ChangeWatcher<\/span>;<br \/>\n<br \/>\n<span class=\"kw3\">public<\/span> <span class=\"kw2\">var<\/span> watcherInstance:ChangeWatcher;<br \/>\n<br \/>\n<span class=\"br0\">&#91;<\/span>Bindable<span class=\"br0\">&#93;<\/span><br \/>\n<span class=\"kw3\">public<\/span> <span class=\"kw2\">var<\/span> model:Model;<br \/>\n<br \/>\n<span class=\"kw3\">public<\/span> <span class=\"kw2\">function<\/span> init<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>:<span class=\"kw3\">void<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n<span class=\"co1\">\/\/this is useful if you want to unwatch or stop detecting changes to this variable<\/span><br \/>\n<span class=\"co1\">\/\/ofcourse init function has to be called<\/span><br \/>\nmodel = <span class=\"kw2\">new<\/span> Model<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\nwatcherInstance = ChangeWatcher.<span class=\"kw3\">watch<\/span><span class=\"br0\">&#40;<\/span>model,<span class=\"br0\">&#91;<\/span><span class=\"st0\">&quot;totalItems&quot;<\/span><span class=\"br0\">&#93;<\/span>,itemsChanged<span class=\"br0\">&#41;<\/span>;<br \/>\n<br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"kw3\">public<\/span> <span class=\"kw2\">function<\/span> itemsChanged<span class=\"br0\">&#40;<\/span>event:PropertyChangeEvent<span class=\"br0\">&#41;<\/span>:<span class=\"kw3\">void<\/span><span class=\"br0\">&#123;<\/span><br \/>\n<span class=\"co1\">\/\/this function is called as soon as value of totalItem changes.<\/span><br \/>\n<span class=\"co1\">\/\/if you want to stop watching this variable.<\/span><br \/>\nwatcherInstance.<span class=\"kw3\">unwatch<\/span><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span>;<br \/>\n<span class=\"br0\">&#125;<\/span><\/div><\/div>\n<p>So you can see that this is very easy way of detecting changes, the only thing to keep in mind is that the variables being watched have to be Bindable. You can also detect changes within chain of classes.<\/p>\n<p>\u00a0<\/p>\n<p>For Eg. ChangeWatcher.watch(model,[&#8220;shoppingCart&#8221;,&#8221;totalItems&#8221;],itemsChanged); the variable totalItems is inside the class ShoppingCart which is initialized in Model. So your chain of variables can be as long as you want.<\/p>\n<p>\u00a0<\/p>\n<p>Other Important thing to note is that the line code after the variable will be executed only after your listener function is called. Its not asynchronous like URLLoader etc.<\/p>\n<p>\u00a0<\/p>\n<p>EDIT: 02\/20\/09 Imp Note:<\/p>\n<p>If you are tracking changes to a number, your changewatcher function will fire only if the number changes. Lets say your variable totalItems is 1 and after performing some calculation its again 1, in this situation your changewatcher won&#8217;t fire because the value of variable hasn&#8217;t changed. It can cause crazy bugs in your application and i have been bitten by this couple of times now.\u00a0<\/p>\n<p>\u00a0<\/p>\n<p>The example below shows how you can watch a chain and also how every variable has to be bindable etc.<\/p>\n<p><a href=\"http:\/\/nayansavla.com\/uploads\/flash\/changewatcher\/srcview\/\" target=\"_blank\">Click here<\/a> for the source.<\/p>\n<p>\u00a0<object width=\"600\" height=\"200\" data=\"http:\/\/nayansavla.com\/uploads\/flash\/changewatcher\/changewatcherex.swf\" type=\"application\/x-shockwave-flash\"><param name=\"wmode\" value=\"opaque\" \/><param name=\"src\" value=\"http:\/\/nayansavla.com\/uploads\/flash\/changewatcher\/changewatcherex.swf\" \/><\/object><\/p>\n<p>\u00a0<\/p>\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Data binding is one of the best things about Flex, frameworks like Cairngorm rely heavily on data binding. But what if you want to detect a change in variable value and then also do something else. Changewatcher comes in handy during times like those. \u00a0 import mx.binding.utils.ChangeWatcher; \u00a0 This is the class i am talking [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[51,66],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-flex","tag-add-new-tag","tag-flex"],"_links":{"self":[{"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=43"}],"version-history":[{"count":0,"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/43\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nayansavla.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}