views:

68

answers:

3

I'm trying to implement a binding between some custom-built models and just beginning to dabble with the whole mx.binding.* collection. I tried this simple, stripped down example, but can't get the binding working correctly. Can somebody tell me where I'm going wrong?

// Model
package  
{
 import flash.events.EventDispatcher;

 public class Model extends EventDispatcher
 {
  private var m_count:uint = 0;

  [Bindable]
  public function get Count():uint
  {
   return this.m_count;
  }

  public function set Count(c:uint):void
  {
   this.m_count = c;
  }
 }
}

And this is what the application MXML looks like

// MXML
<?xml version="1.0" encoding="utf-8"?>
<mx:WindowedApplication xmlns:mx="http://www.adobe.com/2006/mxml" xmlns:core="*" creationComplete="this.init();">
 <mx:Script>
  <![CDATA[
  import flash.events.Event;
  import flash.utils.describeType;
  import mx.binding.utils.ChangeWatcher;

  [Bindable]
  public var model:Model;

  public function init():void
  {
   var _this:Object = this;

   this.addEventListener(Event.ENTER_FRAME, function(e:Event):void {
    _this.model.Count++;
   });


   this.model = new Model();

   trace(ChangeWatcher.canWatch(this.model, "Count")); // This always returns false for some reason
   trace(describeType(this.model));
  }

  public function UpdateText(s:String):void
  {
   trace(s);
  }
  ]]>
 </mx:Script>
 <mx:Text text="{this.model.Count}" creationComplete="trace(this);" />
</mx:WindowedApplication>

Update: I tried an even more bare-bones version as shown below.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="this.m_init();">
    <mx:Script>
        <![CDATA[
        import mx.binding.utils.ChangeWatcher;

        [Bindable] public var m:Object = new Object();

        public function m_init():void
        {
            trace(ChangeWatcher.canWatch(this, "m"));
        }
        ]]>
    </mx:Script>
    <mx:Text text="{this.m}" />
</mx:Application>

Still. Doesn't. Work. ChangeWatcher.canWatch still returns false, although the textfield does display [object Object].

A: 
  public function init():void
  {
    this.addEventListener(Event.ENTER_FRAME, increment);
    this.model = new Model();
  }
  public function increment(e:Event):void 
  {
    if(this.model)
      this.model.Count++;
  }
Amarghosh
I should have been more specific. On line 19 in Main.mxml I am checking if the Count property can be watched using ChangeWatcher.canWatch. This always returns a false, even though the describeType call in the next line shows Count as a public accessor.
Pranav Negandhi
@Pranav In the first example, is the value of `Text` getting updated?
Amarghosh
That's the funny part. The textfield ( <mx:Text text="{this.model.Count}" creationComplete="trace(this);" />) updates with the new value. But I cannot bind the value from the model to UpdateText using ChangeWatcher.watch(). The isWatching() method on CW object that is returned is always false.
Pranav Negandhi
A: 

hi, i can give u one example of changeWatcher, that may be some help, right now i m working with this, it's helpful,

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
<mx:Style source="../assets/css/scaleCSS.css"/>
<mx:Script>
 <![CDATA[
  import mx.binding.utils.ChangeWatcher;
  import mx.controls.Text;
  private var text:Text;

  [Bindable]private var ti1mc:int=50;

  private function init():void
  {
   ChangeWatcher.watch(ti1,'text',updateTextti);
   ChangeWatcher.watch(ti2,'text',updateTextti);
   ChangeWatcher.watch(ti3,'text',updateTextti);
   ChangeWatcher.watch(ti4,'text',updateTextti);
   ChangeWatcher.watch(ti5,'text',updateTextti);
  }

  private function updateTextti(event:Event):void
  {
   var str:String;
   str=event.currentTarget.id.slice(2,3);
//   trace("str : "+str);
//   trace(Canvas(textbox.getChildAt(int(TextInput(event.currentTarget).id.slice(2,3))-1)).id);
   Text(Canvas(textbox.getChildAt(int(TextInput(event.currentTarget).id.slice(2,3))-1)).getChildAt(0)).text=event.currentTarget.text;
//   trace(Text(Canvas(textbox.getChildAt(int(TextInput(event.currentTarget).id.slice(2,3))-1)).getChildAt(0)).id);
//   trace(event.currentTarget.id.slice(2,3));
    if(Canvas(textbox.getChildAt(int(TextInput(event.currentTarget).id.slice(2,3))-1)).width>=textbox.width)
    event.currentTarget.maxChars=event.currentTarget.length;
   else
    event.currentTarget.maxChars=50;
  } 
 ]]>
</mx:Script>
 <mx:Canvas width="80%" height="80%" horizontalCenter="0" verticalCenter="0" borderStyle="solid">
  <mx:VBox id="textbox" height="300" width="200" borderStyle="solid" top="50" left="100" horizontalScrollPolicy="off" verticalScrollPolicy="off">
   <mx:Canvas id="textcan1" borderStyle="solid" borderColor="#FF0000">
    <mx:Text id="text1" fontFamily="Arbeka" styleName="text" rotation="20"/>
   </mx:Canvas>
   <mx:Canvas id="textcan2" borderStyle="solid" borderColor="#FFFF00">
    <mx:Text id="text2" fontFamily="Arbeka" styleName="text" rotation="20"/>
   </mx:Canvas>
   <mx:Canvas id="textcan3" borderStyle="solid" borderColor="#00FF00">
    <mx:Text id="text3" fontFamily="Arbeka" styleName="text" rotation="20"/>
   </mx:Canvas>
   <mx:Canvas id="textcan4" borderStyle="solid" borderColor="#0000FF">
    <mx:Text id="text4" fontFamily="Arbeka" styleName="text" rotation="20"/>
   </mx:Canvas>
   <mx:Canvas id="textcan5" borderStyle="solid" borderColor="#00FFFF">
    <mx:Text id="text5" fontFamily="Arbeka" styleName="text" rotation="20"/>
   </mx:Canvas>
  </mx:VBox>
  <mx:VBox id="textinputbox" height="300" width="200" top="50" right="100" borderStyle="solid" verticalGap="0">
   <mx:TextInput id="ti1" width="100%" maxChars="50"/>
   <mx:TextInput id="ti2" width="100%" maxChars="50"/>
   <mx:TextInput id="ti3" width="100%" maxChars="50"/>
   <mx:TextInput id="ti4" width="100%" maxChars="50"/>
   <mx:TextInput id="ti5" width="100%" maxChars="50"/>
  </mx:VBox>  
 </mx:Canvas>
</mx:Application>

here ti1(textinput) text property is being watched, if there is a change in to the property, then the handler function(updatetextti) will be called

hope this helps

Ankur Sharma
sorry i just copied my code, plzz fetch the useful stuff from this
Ankur Sharma
Thanks Ankur. But I've got it figured out that this is not a syntax issue. Check update in comment to Sam's reply.
Pranav Negandhi
k, dear, i didn't read n e answer actually, if some one i found gettting stuck in changwatcher, i want to help that person, are bhai me and ma frnd, hum bh 2-3 changewatcher sunke pareshaan rahe the, bad mein changewatcher came out to very easy...
Ankur Sharma
A: 

I removed all files inside the obj folder under the project and the problem seems to have gone away. It might have been that FlashDevelop was using older compiled binaries out of this folder, which made the output go out of sync with the source code.

Evidence to support this lies in two things. Changing the SDK did not affect the output on my own computer, which ruled out any SDK-specific issues. So I created a new project in another IDE on the same computer and copy-pasted the code into that one. This time it worked. That's when I thought it might be the cache and went in and deleted it.

In hindsight, I should have renamed it, and later tried to compile with the old cache in place again. That would have been conclusive.

Pranav Negandhi