1 /**
  2  * @name $.ui.simplicityRenderParamsSearchResults
  3  * @namespace Widget that performs an AJAX request for rendered search results. This
  4  * widget is useful for configurations that require a second round trip to a server
  5  * to fetch the formatted result sets.
  6  * <p>
  7  * For optimal search performance, if your search controller can inject the search
  8  * results as HTML into it's response then consider using <code>simplicitySearchResults</code> instead.
  9 */
 10 (function ($) {
 11   $.widget("ui.simplicityRenderParamsSearchResults", $.ui.simplicityWidget, {
 12     /**
 13      * Widget options.
 14      *
 15      * <dl>
 16      *   <dt>searchElement</dt>
 17      *   <dd>
 18      *     The simplicityDiscoverySearch widget that this widget binds it's events to. Defaults to <code>'body'</code>.
 19      *   </dd>
 20      *   <dt>url</dt>
 21      *   <dd>
 22      *     The url from which to get the rendered results.
 23      *   </dd>
 24      * </dl>
 25      * @name $.ui.simplicityRenderParamsSearchResults.options
 26      */
 27     options: {
 28       searchElement: 'body',
 29       url: ''
 30     },
 31     _create : function () {
 32       this
 33         ._addClass('ui-simplicity-render-params-search-results')
 34         ._bind(this.options.searchElement, 'simplicitySearchResponse', this._searchResponseHandler);
 35     },
 36     /**
 37      * Event handler for the <code>simplicitySearchResponse</code> event. Expects the given
 38      * search response object to contain a <code>_discovery.response.renderParameters</code>
 39      * field which it loads via <code>$.load</code> into the page.
 40      *
 41      * @name $.ui.simplicityRenderParamsSearchResults._searchResponseHandler
 42      * @function
 43      * @private
 44      */
 45     _searchResponseHandler: function (evt, searchResponse) {
 46       var renderParameters = '';
 47       if (searchResponse && searchResponse._discovery && searchResponse._discovery.response) {
 48         var discoveryResponse = searchResponse._discovery.response;
 49         renderParameters = discoveryResponse.renderParameters || '';
 50       }
 51       if (renderParameters === '') {
 52         this.element.html('');
 53       } else {
 54         var url = this.options.url + '?' + renderParameters;
 55         this.element.load(url, null, $.proxy(function (responseText, statusText, xhr) {
 56           if (xhr.status < 200 || xhr.status >= 300) {
 57             var error = $('<div class="ui-state-error-text"/>');
 58             error.text('[' + xhr.status + '] ' + xhr.statusText + '\n' + responseText);
 59             this.element.append(error);
 60           }
 61         }, this));
 62       }
 63     }
 64   });
 65 }(jQuery));
 66