Thursday, September 20, 2007

Map representation in Spring & Javascript


<bean id="action2Dashboard" class="java.util.HashMap" singleton="true">
   <constructor-arg>
     <map>
       <entry key="/admin/license/installLicense.action">
       <value>system:administration_setupsupport.3</value>
       </entry>
       <entry key="/admin/agent/listAgents.action">
       <value>system:administration_agents.5</value>
     </entry>
     </map>
   </constructor-arg>
</bean>


self.dashboards = {
  "/admin/license/installLicense.action" : "system:administration_setupsupport.3",
  "/admin/agent/listAgents.action" : "system:administration_agents.5"
}

var dashboard = dashboards["/admin/licnese/installLicense.action"];

Thursday, September 13, 2007

Publish/Subscribe



NotificationService {
publish(topic, payload);
subscribe(topic, listener);
}

Internally, each topic maintain a set of listener; Alternatively, topic is not used, uses more specific method such as addMouseClickListener(...) to specify it implicitly.

Callee subscribe/unsubscribe the listeners to interesting topic; When something happend (e.g, UI interaction, object added, removed or modified), event is fired, (The event carries all the context information such as source object, action, context etc that listener need to know). publish() is invoked to notify listener.

Listener {
ObjectChanged(event);
}

Publish/Subscribe also known as Observer pattern. The Observer pattern defines an one-to-many dependency between a subject object and any number of observer objects so that when the subject object changes state, all its observer objects are notified and updated automatically.

In the diagram, we can see it doesn't pass an event/data/msg object though, one static reference to subject object is kept in concreteObserver to get the latest state information.

Tuesday, September 11, 2007

Javascript keyword "this"


function doSomething() {
this.style.color = '#cc0000';
}

In JavaScript this always refers to the “owner” of the function we're executing, or rather, to the object that a function is a method of. hen we define our faithful function doSomething() in a page, its owner is the page, or rather, the window object (or global object) of JavaScript.

An onclick property, though, is owned by the HTML element it belongs to.

element.onclick = doSomething;

The function is copied in its entirety to the onclick property (which now becomes a method). So if the event handler is executed this refers to the HTML element and its color is changed.

However, if you use inline event registration

<element onclick="doSomething()">

you do not copy the function! Instead, you refer to it, the this keyword once again refers to the global window

Thursday, September 6, 2007

Javacript location reload

- To refresh current iframe


parent.frames['contentBox'].location.reload();

- To refresh current page

document.location.reload()

one good example, after ajax session timeout, invoking this will be taken to logon page.

Wednesday, September 5, 2007

Struts 2 Architecture





At its core, Struts 2 is a Command pattern implementation. The framework encapsulate the execution of the action. Because you 're calling actions through a framework, you can configure the framework to add services around the call.

The ActionInvocation object provides access to the runtime environment such as action, interceptor, context (request parameters, session parameters, user locale, etc), result. It represents the current state of the execution of the action. e.g., it maintain the state and know which interceptor has been invoked.

ActionProxy serves as client code's handler.
ActionProxy proxy = ActionProxyFactory.getFactory().createActionProxy(namespace, actionName, context)

Tuesday, September 4, 2007

Javascript Object Literal

- Use object literals as flexible function parameters

Object literals are objects defined using braces ({}) that contain a set of comma separated key value pairs much like a map in Java.


{key1: "stringValue", key2: 2, key3: ['blue','green','yellow']}

The example above shows an object literal which contains a string, number, and array of strings. As you may imagine object literals are very handy in that they can be used as generic for passing in arguments to a function. The function signature does not change if you choose to require more properties in a function. Consider using object literal as the parameters for methods.

function doSearch(serviceURL, srcElement, targetDiv) {
var params = {service: serviceURL, method: "get", type: "text/xml"};
makeAJAXCall(params);
}


function makeAJAXCall(params) {
var serviceURL = params.service;
...
}

Also note that with object literals you can pass anonymous functions as may be seen in the following example:

function doSearch() {
makeAJAXCall({serviceURL: "foo",
method: "get",
type: "text/xml",
callback: function(){alert('call done');}
});
}

function makeAJAXCall(params) {
var req = // getAJAX request;
req.open(params.serviceURL, params.method, true);
req.onreadystatechange = params.callback;
...
}


Object literals should not be confused with JSON which has similar syntax. JSON is a subset of object literal notation of javascript.


self.dashboards = {
"/admin/license/installLicense.action" : "system:administration_setupsupport.3",
"/admin/agent/listAgents.action" : "system:administration_agents.5"
}

*** when needed, say there is '/' or space character, we can use double quote to escape it.