Reuters tutorial: step 6

jpmckinney edited this page Apr 11, 2013 · 15 revisions

Table of Contents

Solr handles free-text searches with a q parameter. Create a new widget, TextWidget.js, inheriting from AbstractTextWidget, which is designed to handle the q parameter:

(function ($) {
AjaxSolr.TextWidget = AjaxSolr.AbstractTextWidget.extend({
});
})(jQuery);

And add the JavaScript file:

<script src="widgets/TextWidget.js"></script>

Now, add an instance of the widget to the Manager in reuters.js:

Manager.addWidget(new AjaxSolr.TextWidget({
  id: 'text',
  target: '#search'
}));

Let’s implement the abstract methods init and afterRequest, which should be familiar now:

init: function () {
  var self = this;
  $(this.target).find('input').bind('keydown', function(e) {
    if (e.which == 13) {
      var value = $(this).val();
      if (value && self.set(value)) {
        self.doRequest();
      }
    }
  });
},

afterRequest: function () {
  $(this.target).find('input').val('');
}

Unlike the tagcloud widget, we cannot use the handy clickHandler API method in the jQuery bind function, because the bind and click events behave differently. Instead, we use the AbstractTextWidget set API method directly. set returns true if the query was changed (if the query is set to the same value as before, it returns false). Here, if it returns true, the widget sends a request the Solr.

[What we have so far]

And there we have it, a free-text widget. Now, let’s add autocompletion.

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.