You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Andun Sameera <an...@gmail.com> on 2013/08/12 08:22:10 UTC

renderHead vs Behavior Constructor

Hi all,

I have implemented a custom Behviour called WhiteboardBehavior extending
the AbstractDefaultAjaxBehavior. In that class I have a static HashMap like
this,

private static ConcurrentHashMap<Integer,Element> elementMap=new
ConcurrentHashMap<Integer,Element>();

In the constructor of my Behavior class I put elements to the map like this,

public WhiteboardBehavior(String whiteboardId,String whiteboardContent){
        super();
        this.whiteboardId=whiteboardId;
        Element element=new Element(whiteboardContent);

        if(element!=null){
            elementMap.put(element.getId(),element);
        }
}

I am trying to execute some JavaScript codes int he renderHead method using
the above added elements. I did it like this,

public void renderHead(Component component, IHeaderResponse response) {
        super.renderHead(component,response);

        initReferences(response);
        String callbackUrl=getCallbackUrl().toString();
        String whiteboardInitializeScript="" +
                "callbackUrl='"+callbackUrl+"';\n" +
                "whiteboard = bay.whiteboard.Create();\n" +
                "elementCollection=whiteboard.getMainCollection();\n"+
                "whiteboard.getMainCollection().onChange =
function(element){\n"+
                "changedElement=this.getJson(element);\n"+

"Wicket.Ajax.get({u:'"+callbackUrl+"',ep:{editedElement:changedElement}});\n};\n"+

"whiteboard.render(document.getElementById('"+whiteboardId+"'));\n"+
                "whiteboard.setBoundaries(0, 0, 0, 0);\n";

        //Clearing the whiteboard for first client
        IWebSocketConnectionRegistry reg =
IWebSocketSettings.Holder.get(Application.get()).getConnectionRegistry();
        if(reg.getConnections(Application.get()).size()==0){
            elementMap.clear();
        }

       * //Loading existing content for clients join after first one
        if(!elementMap.isEmpty()){
            Map<Integer,Element> sortedElementList = new
TreeMap<Integer,Element>(elementMap);
            JSONArray jsonArray=new JSONArray();
            for (Element e : sortedElementList.values()) {
                jsonArray.put(e.getJSON());
            }

whiteboardInitializeScript+="elementCollection.parseJson('"+jsonArray.toString()+"');";
        }*


response.render(OnDomReadyHeaderItem.forScript(whiteboardInitializeScript));
}

But when renderHead is executed for the first time, the elementMap is
empty. What has gone wrong here?

Thanks!

-- 
Regards
Andun S.L. Gunawardana
Undergraduate
Department of Computer Science And Engineering
University of Moratuwa
Sri Lanka

Blog - http://www.insightforfuture.blogspot.com/
LinkedIn - http://www.linkedin.com/pub/andun-s-l-gunawardana/34/646/703
Twitter -http://twitter.com/AndunSLG

Re: renderHead vs Behavior Constructor

Posted by Andun Sameera <an...@gmail.com>.
Thanks Martin! It worked.

On Mon, Aug 12, 2013 at 1:59 PM, Martin Grigorov <mg...@apache.org>wrote:

> Hi,
>
>
> On Mon, Aug 12, 2013 at 9:22 AM, Andun Sameera <an...@gmail.com> wrote:
>
> > Hi all,
> >
> > I have implemented a custom Behviour called WhiteboardBehavior extending
> > the AbstractDefaultAjaxBehavior. In that class I have a static HashMap
> like
> > this,
> >
> > private static ConcurrentHashMap<Integer,Element> elementMap=new
> > ConcurrentHashMap<Integer,Element>();
> >
> > In the constructor of my Behavior class I put elements to the map like
> > this,
> >
> > public WhiteboardBehavior(String whiteboardId,String whiteboardContent){
> >         super();
> >         this.whiteboardId=whiteboardId;
> >         Element element=new Element(whiteboardContent);
> >
> >         if(element!=null){
> >             elementMap.put(element.getId(),element);
> >         }
> > }
> >
> > I am trying to execute some JavaScript codes int he renderHead method
> using
> > the above added elements. I did it like this,
> >
> > public void renderHead(Component component, IHeaderResponse response) {
> >         super.renderHead(component,response);
> >
> >         initReferences(response);
> >         String callbackUrl=getCallbackUrl().toString();
> >         String whiteboardInitializeScript="" +
> >                 "callbackUrl='"+callbackUrl+"';\n" +
> >                 "whiteboard = bay.whiteboard.Create();\n" +
> >                 "elementCollection=whiteboard.getMainCollection();\n"+
> >                 "whiteboard.getMainCollection().onChange =
> > function(element){\n"+
> >                 "changedElement=this.getJson(element);\n"+
> >
> >
> >
> "Wicket.Ajax.get({u:'"+callbackUrl+"',ep:{editedElement:changedElement}});\n};\n"+
> >
> > "whiteboard.render(document.getElementById('"+whiteboardId+"'));\n"+
> >                 "whiteboard.setBoundaries(0, 0, 0, 0);\n";
> >
> >         //Clearing the whiteboard for first client
> >         IWebSocketConnectionRegistry reg =
> > IWebSocketSettings.Holder.get(Application.get()).getConnectionRegistry();
> >         if(reg.getConnections(Application.get()).size()==0){
> >             elementMap.clear();
> >         }
> >
>
> I guess the map is cleared here ^^.
> The web socket connections are opened after the page is loaded.
>
>
> >
> >        * //Loading existing content for clients join after first one
> >         if(!elementMap.isEmpty()){
> >             Map<Integer,Element> sortedElementList = new
> > TreeMap<Integer,Element>(elementMap);
> >             JSONArray jsonArray=new JSONArray();
> >             for (Element e : sortedElementList.values()) {
> >                 jsonArray.put(e.getJSON());
> >             }
> >
> >
> >
> whiteboardInitializeScript+="elementCollection.parseJson('"+jsonArray.toString()+"');";
> >         }*
> >
> >
> >
> >
> response.render(OnDomReadyHeaderItem.forScript(whiteboardInitializeScript));
> > }
> >
> > But when renderHead is executed for the first time, the elementMap is
> > empty. What has gone wrong here?
> >
> > Thanks!
> >
> > --
> > Regards
> > Andun S.L. Gunawardana
> > Undergraduate
> > Department of Computer Science And Engineering
> > University of Moratuwa
> > Sri Lanka
> >
> > Blog - http://www.insightforfuture.blogspot.com/
> > LinkedIn - http://www.linkedin.com/pub/andun-s-l-gunawardana/34/646/703
> > Twitter -http://twitter.com/AndunSLG
> >
>



-- 
Regards
Andun S.L. Gunawardana
Undergraduate
Department of Computer Science And Engineering
University of Moratuwa
Sri Lanka

Blog - http://www.insightforfuture.blogspot.com/
LinkedIn - http://www.linkedin.com/pub/andun-s-l-gunawardana/34/646/703
Twitter -http://twitter.com/AndunSLG

Re: renderHead vs Behavior Constructor

Posted by Martin Grigorov <mg...@apache.org>.
Hi,


On Mon, Aug 12, 2013 at 9:22 AM, Andun Sameera <an...@gmail.com> wrote:

> Hi all,
>
> I have implemented a custom Behviour called WhiteboardBehavior extending
> the AbstractDefaultAjaxBehavior. In that class I have a static HashMap like
> this,
>
> private static ConcurrentHashMap<Integer,Element> elementMap=new
> ConcurrentHashMap<Integer,Element>();
>
> In the constructor of my Behavior class I put elements to the map like
> this,
>
> public WhiteboardBehavior(String whiteboardId,String whiteboardContent){
>         super();
>         this.whiteboardId=whiteboardId;
>         Element element=new Element(whiteboardContent);
>
>         if(element!=null){
>             elementMap.put(element.getId(),element);
>         }
> }
>
> I am trying to execute some JavaScript codes int he renderHead method using
> the above added elements. I did it like this,
>
> public void renderHead(Component component, IHeaderResponse response) {
>         super.renderHead(component,response);
>
>         initReferences(response);
>         String callbackUrl=getCallbackUrl().toString();
>         String whiteboardInitializeScript="" +
>                 "callbackUrl='"+callbackUrl+"';\n" +
>                 "whiteboard = bay.whiteboard.Create();\n" +
>                 "elementCollection=whiteboard.getMainCollection();\n"+
>                 "whiteboard.getMainCollection().onChange =
> function(element){\n"+
>                 "changedElement=this.getJson(element);\n"+
>
>
> "Wicket.Ajax.get({u:'"+callbackUrl+"',ep:{editedElement:changedElement}});\n};\n"+
>
> "whiteboard.render(document.getElementById('"+whiteboardId+"'));\n"+
>                 "whiteboard.setBoundaries(0, 0, 0, 0);\n";
>
>         //Clearing the whiteboard for first client
>         IWebSocketConnectionRegistry reg =
> IWebSocketSettings.Holder.get(Application.get()).getConnectionRegistry();
>         if(reg.getConnections(Application.get()).size()==0){
>             elementMap.clear();
>         }
>

I guess the map is cleared here ^^.
The web socket connections are opened after the page is loaded.


>
>        * //Loading existing content for clients join after first one
>         if(!elementMap.isEmpty()){
>             Map<Integer,Element> sortedElementList = new
> TreeMap<Integer,Element>(elementMap);
>             JSONArray jsonArray=new JSONArray();
>             for (Element e : sortedElementList.values()) {
>                 jsonArray.put(e.getJSON());
>             }
>
>
> whiteboardInitializeScript+="elementCollection.parseJson('"+jsonArray.toString()+"');";
>         }*
>
>
>
> response.render(OnDomReadyHeaderItem.forScript(whiteboardInitializeScript));
> }
>
> But when renderHead is executed for the first time, the elementMap is
> empty. What has gone wrong here?
>
> Thanks!
>
> --
> Regards
> Andun S.L. Gunawardana
> Undergraduate
> Department of Computer Science And Engineering
> University of Moratuwa
> Sri Lanka
>
> Blog - http://www.insightforfuture.blogspot.com/
> LinkedIn - http://www.linkedin.com/pub/andun-s-l-gunawardana/34/646/703
> Twitter -http://twitter.com/AndunSLG
>