Lance | 7 Aug 17:39

DWR and YUI autocomplete

For anyone interested, here's an example of DWR and YUI autocomplete 
working together.
DwrYuiDataSource currently only supports remote methods of the form
    public ReturnType methodName(String query)

But could easily be extended to take beforeArgs and afterArgs so that it 
supports
    public ReturnType methodName(Object beforeArg1, Object beforeArg2, 
String query, Object afterArg1, Object afterArg2)

----------------------------
javascript: DwrYuiDataSource
----------------------------
mypackage.DwrYuiDataSource = function(remoteMethod) {
    this.remoteMethod = remoteMethod;
    this._init();
};

mypackage.DwrYuiDataSource.prototype = new YAHOO.widget.DataSource();

mypackage.DwrYuiDataSource.prototype.doQuery = function(oCallbackFn, 
sQuery, oParent) {
    var oSelf = this;
    this.remoteMethod(sQuery, function(aResults) {
        var resultObj = {};
        resultObj.query = decodeURIComponent(sQuery);
        resultObj.results = aResults;
        oSelf._addCacheElem(resultObj);       
        oSelf.getResultsEvent.fire(oSelf, oParent, sQuery, aResults);
        oCallbackFn(sQuery, aResults, oParent);
    });
};

----------------------
html
----------------------
<div id="contactsAutoComplete">
    <input type="text" id="contactsQuery" />
    <div id="contactsResultsContainer"></div>
</div>

-----------------------
javascript: autocomplete
-----------------------
var dataSource = new mypackage.DwrYuiDataSource(ContactsAjaxActions.find);
var autoComplete = new YAHOO.widget.AutoComplete('contactsQuery', 
'contactsResultsContainer', dataSource);

autoComplete.formatResult = function(contact, query) {
    return contact.name;
};

autoComplete.itemSelectEvent.subscribe(function(eventType, eventArgs) {
    var contact = eventArgs[2];
    ContactsAjaxActions.addContact(contact.contactId, function(data) {
        ...
    });
});

----------------------
java: ContactsAjaxActions.java
----------------------
public Contact[] find(String contactQuery) {
    // seatch logic here
    // Contact has contactId and name
}

public void addContact(int contactId) {
}

Cheers,
Lance.

Gmane