DWR: Frequently Asked Questions

DWR: Frequently Asked Questions

Answers to common questions about DWR, the technology used to provide AJAX support.

What is the best way to learn about DWR?

http://directwebremoting.org/dwr/documentation

How are Spring and DWR connected?

Jive SBS configures DWR with Spring.  Look at spring-dwrContext.xml for the configurations.  See this DWR and Spring documentation for more details.

Here is an example Spring config:

    <bean id="remoteCommunityUtils" class="com.jivesoftware.community.dwr.RemoteCommunityUtils" parent="remoteSupport">
        <dwr:remote javascript="CommunityUtils">
            <dwr:include method="setUserCommunityListProperty"/>
            <dwr:include method="closeCustomizeWidgetMessageProperty"/>
            <dwr:include method="setUserDefaultTab"/>
        </dwr:remote>
    </bean>

In the above example, the element <dwr:remote javascript="CommunityUtils" defines the name of the Javascript file/object you refer to in your theme code.

To make use of this, you'd put this in your file:

<@resource.dwr file="CommunityUtils"/>

And you'd make a call like this:

<@resource.javascript>CommunityUtils.setUserDefaultTab("Overview")</@resource.javascript>

What are some DWR best practices?

You should always explicitly include what methods you want to expose through your javascript using the <dwr:include> element.  Otherwise the default is to expose all the methods, which can present huge security risks.  Even if things are ok now, any methods people add in the future will be exposed, so it's safest to be explicity.

How do you make use of DWR in your theme template?

Here is an example of what you need to do to use the Watches remote object in your template:

<@resource.dwr utils="true" file="Watches"/>

How do I get access to common methods/objects from the DWR context?

In your Spring configuration extend the Spring Bean remoteSupport.  Here is the remoteSupport definition and what's available there:

    <bean id="remoteSupport" class="com.jivesoftware.community.dwr.RemoteSupport" abstract="true">
        <property name="authenticationProvider" ref="authenticationProvider"/>
        <property name="userManager" ref="userManager"/>
        <property name="localeManager" ref="localeManager"/>
    </bean>

And here is an example definition extending it (parent attribute):

    <bean id="remoteCommunityUtils" class="com.jivesoftware.community.dwr.RemoteCommunityUtils" parent="remoteSupport">
        <dwr:remote javascript="CommunityUtils">
            <dwr:include method="setUserCommunityListProperty"/>
            <dwr:include method="closeCustomizeWidgetMessageProperty"/>
            <dwr:include method="setUserDefaultTab"/>
        </dwr:remote>
    </bean>

Think of this as the DWR context equivalent of extending JiveActionSupport for your Action classes.

How do I get i18n text into the DWR context?

RemoteSupport has methods for getting i18n text in the dwr context (since you don't have a struts action like usual).

How do I use callbacks with DWR?

Here's an example:

OpenSearchQuery.getSearchResultsByEngineID(${engine.ID?c}, osSearchTerm, {
                   callback:function(data) {
                       writeOSResults(data, ${engine.ID?c});
                    },
                    errorHandler:function(errorString, exception) {
                        $("jive-search-opensearch-${engine.ID?c}").innerHTML = "<span style=\"color:red\"><@s.text name="search.results.os.error.label" /></span>";
                     }});

If a DWR method is returning something, then that javascript method call accepts a callback closure as the final argument.  The example above shows an example with the callback function, and an explicit errorHandler function.  The default behavior if you don't specify an errorHandler is to throw up a javascript alert, so while optional, the errorHandler can improve your user experience.