Start a new topic

The POWER of 'dynamic' and 'Js.referenceAs'

These TWO features of DuoCode are AMAZING.


I keep referring to GWT, because that is the currently the grand daddy of compiled

 (permutations even) javascript application.


Anyways... There is something they use called GWT JSNI (JavaScript Native Interface). it allows native client to "interop" with the compiled (non readable) javascript. This meant there was always a "THUNKING" layer you had to go through

to interop with native javascript. Not to mention "BOXING" you had to BOX and UNBOX your java pojo to and from native JavaScriptObjects. Now to wrap an javascript script object you would have to subclass a concrete java class called 'JavAScriptObject' and your would actually have to implement the body of the javascript object, like this:


package com.gateway.ajax.client;


import com.google.gwt.core.client.JavaScriptObject;

import com.google.gwt.core.client.JsArrayMixed;


/**

 * AJAX-RPC 1.0 wire token for a {@link com.gateway.ajax.client.AjaxRpcService} implementation

 */

public final class AjaxRpcToken extends JavaScriptObject

{

 protected AjaxRpcToken() {}

 /**

  * The name of the method to execute for a {@link com.gateway.ajax.client.AjaxRpcService} implementation

  * 

  * @return name of the method to execute for a {@link com.gateway.ajax.client.AjaxRpcService} implementation

  */

 public native String getMethod() /*-{ return this.method; }-*/;

 public native void setMethod(String method) /*-{ this.method = method; }-*/;


 /**

  * The parameters for the method to execute for a {@link com.gateway.ajax.client.AjaxRpcService} implementation

  * 

  * @return paramters for the method to execute for a {@link com.gateway.ajax.client.AjaxRpcService} implementation

  */

 public native JsArrayMixed getParams() /*-{ return this.params; }-*/;

 public native void setParams(JsArrayMixed params) /*-{ this.params = params; }-*/;

 /**

  * The request id of the method to execute for a {@link com.gateway.ajax.client.AjaxRpcService} implementation

  * 

  * @return request id of the method to execute for a {@link com.gateway.ajax.client.AjaxRpcService} implementation

  */

 public native String getId() /*-{ return this.id; }-*/;

 public native void setId(String id) /*-{ this.id = id; }-*/;




AS you can see, it can very tedious try to wrap every property and method including the underlying java script in your /*-{ ... }*-/ JSNI block to actually do the interop. Now in DuoCode, that same class looks like this:


 public class RemoteRequest

 {

        [Js(Name = "ajaxrpc")]

        public string ajaxrpc { get; set; }


        [Js(Name = "method")]

        public string method { get; set; }


        [Js(Name = "parameters")]

        public object[] parameters { get; set; }


        [Js(Name = "id")]

        public string id { get; set; }

 }


Simple, Clean Elegant C# class... SO KOOL!!!


Now to the Js.reference as... In GWT (this time i wont bore you with the java code)

i had to manually create java-to-javascript Object Serialization so i can basically

take a method name and array of objects parameters and send them to the server

for remote method invocation. This TOOK ALOT of code to do ... trust me, pages of multi filed classes to handle all the different (and javascriptobject castable) objects and serialize all this stuff to a JSON string to go over the wire to the server.


In my upcoming AjaxGateway... ALL OF THAT has been reduced to a function with only TWO lines of clean c# code, using Js.referenceAs and the native object

serialization of the DuoCode framework. Take a look:


public string FormatRequest(string methodName, object[] paramValues)

 {

            RemoteRequest rpcRequest = new RemoteRequest() { ajaxrpc = "1.0", method = methodName, parameters = paramValues, id = "0" };

            return Js.referenceAs<string>(@"JSON.stringify(rpcRequest)");

 }


 As you can see, im just filling the properties for a plain old C# class (including something as general as a object array) and one DuoCode function handles all the rest.


Please excuse my language, but WTF!!!


That is unbelievable... After only a fews days... I have my whole framework ported to use LESS, CLEANER NATIVE C# that does all the stuff i was using GWT to do... The GWT framework WHOLE purpose is to create AJAX web applications using a strong typed language that compiles to native javascript. It also has all

the DOM manipulation stuff. But for the Java Developer, its biggest selling point was that a java developer can create and the same Remote Service (GWT-RPC) code and DTO's for creating these ajax web apps. and it also featured a rich UI (although they are replacing that with the basic feature of [JsType] attribute that is more like the DuoCode [Js] and exposing native javascript via a Bindings Of Sorts so they can use various WebComponents. Polymer i think they call that project).


All that being said... 'dynamic' and Js.referenceAs' are game changers!!!


Login or Signup to post a comment