i think in this particular case it is better to dump the _javascript_ into the output stream of the response instead of buffering it into a string or into a templating engine.

a cleaner approach would be to parameterize this function if its missing any params and break it out into a js file and serve it as a packaged resource.

-Igor




On 2/1/06, Gavin <[EMAIL PROTECTED]> wrote:
I have been playing with Wicket and had a few ideas for improvements.

1) Introduce JSON. This converts java objects (models, maps, etc) into
_javascript_ objects. This is a much easier way of initialising _javascript_
(ajax) components. All this code exists in open source already (see Java
JSON-RPC)

For example

        List list = new ArrayList();
        list.add("string");
        list.add(new Integer(100));
        list.add(new Double(100));
        list.add(new Date());

        Map firstMap = new LinkedHashMap();
        firstMap.put("stringProp", "string");
        firstMap.put("intProp", new Integer(100));
        firstMap.put("doubleProp", new Double(100));
        firstMap.put("dateProp", new Date());
        firstMap.put("booleanProp", Boolean.TRUE);
        firstMap.put("beanProp", new Bean());

        firstMap.put("trickyStringProp", "_\"_'_\t_\n_\\_");

        firstMap.put("listProp", list);
        String jsonString = ser.toJSONString(firstMap);

       Creates....
{"javaClass":"java.util.LinkedHashMap","map":{"doubleProp":100,"trickyString
Prop":"_\"_'_\t_\n_\\_","listProp":{"list":["string",100,100,{"javaClass":"j
ava.util.Date","time":1.138678457762e12}],"javaClass":"java.util.ArrayList"}
,"intProp":100,"stringProp":"string","beanProp":{"doubleProp":100,"javaClass
":"proj.Bean","intProp":100,"stringProp":"string","dateProp":{"javaClass":"j
ava.util.Date","time":1.138678457762e12}},"booleanProp":true,"dateProp":{"ja
vaClass":"java.util.Date","time":1.138678457762e12}}};

which can then be inserted into the html page and used by the _javascript_
code. It can even be sent back via XmlHttpRequest (to enable easy
roundtripping of models) and converted back in java objects.

2) Introducing XmlResourceBundles (as seen in Java V6). This would work very
well with the Template Engine suggested in point 3.

We could then move all the _javascript_ / html stuff out of java and remove
all the escaping and line wrapping. It is very easy to implement (just
extend the PropertiesFactory to load from an xml file).

No more...(from ImmediateTextField.java)
StringBuffer s = new StringBuffer(
     "\t<script language=\"_javascript_\"
type=\"text/_javascript_\">\n").append(
     "\tfunction immediateCheckBox(componentUrl, componentPath, val) {
\n").append(
     "\t\tdojo.io.bind({\n").append(
     "\t\t\turl: componentUrl + '&' + componentPath + '=' + val,\n").append(
     "\t\t\tmimetype: \"text/plain\",\n").append(
     "\t\t\tload: function(type, data, evt) {}\n" + "\t\t});\n" +
"\t}\n").append(
     "\t</script>\n");
just
getProperty("immediateCheckBox");

3) Introduce a template engine to remove the big (and very ugly) strings
Although we can do this outside the core, having a standard template
solution makes the framework more accessible and standardise the components.

Template engines (Freemarker, velocity, etc) contruct strings with ease. It
also means you can change strings (if you have watch and reload xml property
files) without recompiling.

For example..(for FXOnClickHighlighter)

Map context = new HashMap();
context.put("component", this);
String TemplateEngine.render("highlighterIn.ftl",context);
String TemplateEngine.render("highlighterOut.ftl",context);

or with XmlPropertyFiles....

String TemplateEngine.render(getProperty("highlighterIn"),context);
String TemplateEngine.render (getProperty("highlighterOut"),context);

could replace:

  // String to be written to the header
  String s;
  // dojo function calls for highlight/unhighlight
  String highlightInFunction;
  String highlightOutFunction;

  // set the correct dojo functions for the type of highlighter
  if (type == "c2c")
  {
   highlightInFunction = "dojo.fx.html.colorFade(node, " +
startColor.toString() + ","
     + endColor.toString() + ", duration, function(){" + componentId
     + "_highlighterState='highlighted';});";
   highlightOutFunction = "dojo.fx.html.colorFade (node, " +
endColor.toString() + ","
     + startColor.toString() + ", duration, function(){" + componentId
     + "_highlighterState='unhighlighted';});";
  }
  else
  {
   highlightInFunction = "dojo.fx.html.colorFadeOut(node, " +
endColor.toString()
     + ", duration ,0,function(){" + componentId
     + "_highlighterState='highlighted';});";
   highlightOutFunction = "dojo.fx.html.colorFadeOut(node, startbc, duration
,0,function(){"
     + componentId + "_highlighterState='unhighlighted';});";
  }

  s = "\t<script language=\"_javascript_\" type=\"text/_javascript_\">\n" + "\t"
+ componentId
    + "_highlighterState = 'unhighlighted'; \n" + "\t" + componentId
    + "_first = false; \n" + "\tfunction " + componentId
    + "_highlight(id, duration) { \n" + "\t\tif(" + componentId
    + "_highlighterState!='highlighting'){\n"
    + "\t\t\tnode = document.getElementById(id);\n" +

    "\t\t\tif(!" + componentId + "_first){\n" + "\t\t\t" + componentId
    + "_first = true; \n" + "\t\t\t\tstartbc =
dojo.html.getBackgroundColor(node);\n"
    + "\t\t\t}\n" + "\t\t\tif(" + componentId
    + "_highlighterState == 'unhighlighted') \n" + "\t\t\t{ \n" + "\t\t\t\t"
    + componentId + "_highlighterState = 'highlighting';\n" + "\t\t\t\t"
    + highlightInFunction + "\n" + "\t\t\t} else {\n" + "\t\t\t\t" +
componentId
    + "_highlighterState = 'highlighting';\n" + "\t\t\t\t" +
highlightOutFunction
    + "\n" + "\t\t\t}\n" + "\t\t}\n" + "\t}\n" + "\t</script>\n";

Cheers

PS: Great framework!!






-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=103432&bid=230486&dat=121642
_______________________________________________
Wicket-develop mailing list
Wicket-develop@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wicket-develop

Reply via email to