Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session

hello world!

Avatar

Level 2
i just started using cocomo, actually just started using Flex
builder a trial version :-)

so i think doing hello world is traditional start.



im trying to popup hello world message for all users,

any user click the button a hello world message will popup
for all users.



Here is what I come up with



<sharedModel:SharedProperty id="sp"
change="onChange(event)"/>

<mx:Button label="Button" click="onClick(event)"/>



<mx:Script>



<![CDATA[



import mx.controls.Alert;

import com.adobe.rtc.sharedModel.SharedProperty;

import com.adobe.rtc.events.SharedPropertyEvent;





private function onChange(e:SharedPropertyEvent):void

{

Alert.show("Hello World!")

}







private function onClick(e:MouseEvent):void

{

sp.subscribe()

sp.value += 1 ;

}



]]>



</mx:Script>







it works but it needs twice click form all user at first ?
how to do it correctly?
1 Reply

Avatar

Former Community Member
Hi,



There are a bunch of things that you can do as best
practices. You should call subscribe on your model first after you
initialize it as that creates the the nodes so that when you assign
the value, the collectionnodes are synchronized and nodes are
already created by then. What we do in case you are trying to
assign a value to shared property when the sharedproperty
collectionnode is not synchronized is that we cache the value and
when your collectionode is syncrhonized , then we publish the
cached value. In your example, you are doing sp.value +=1 where
sp.value is initally NaN and so doing +1 will keep it a NaN and
hence you are assigning a NaN value, which doesnt get cached up.
Your example works second time because, the collectionnode is
already synchronized and nodes are created by the second time, so
even if you assign NaN it just gets published. So, you receive the
onChange event.



Here is what you can do to get it working,



<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="
http://www.adobe.com/2006/mxml"
layout="absolute" xmlns:session="com.adobe.rtc.session.*"

xmlns:authentication="com.adobe.rtc.authentication.*"
xmlns:sharedModel="com.adobe.rtc.sharedModel.*"
creationComplete="onCreationComplete()"

width="500" height="500"

>

<mx:Script>

<![CDATA[

import com.adobe.rtc.events.SharedPropertyEvent;

import com.adobe.rtc.sharedModel.SharedProperty



private var model:SharedProperty ;

import mx.controls.Alert;

import com.adobe.rtc.sharedModel.SharedProperty;

import com.adobe.rtc.events.SharedPropertyEvent;



private function onCreationComplete():void

{

cSession.login();

}

private function onChange(e:SharedPropertyEvent):void

{

Alert.show("Hello World!")

}







private function onClick(e:MouseEvent):void

{

sp.subscribe()

sp.value = 1 ;// you need to assign some value that is not
NaN to get it working if you are calling it //before the
synchronization of collectionnode.

}





]]>

</mx:Script>

<authentication:AdobeHSAuthenticator id="auth"
userName="Your userName" password="Your passwd" />

<session:ConnectSession id="cSession"
authenticator="{auth}" roomURL="Your room Url">







</session:ConnectSession>

<sharedModel:SharedProperty id="sp"
change="onChange(event)" connectSession="{cSession}"/>

<mx:Button label="Button" click="onClick(event)"/>

</mx:Application>



Hope this helps. Let me know if there are any queries