2 Replies Latest reply on Sep 24, 2010 9:45 AM by Kim Eeckhout

    How to override markPrevious and markNext methods on rich:suggestionBox javascript

    Kim Eeckhout Newbie



      I have a requirement where users should be able to navigate from the  first to the last item in a rich:suggestionBox's list by pressing the [Arrow Up]   key, and vice versa by pressing the [Arrow Down].


      I need to get this working on richfaces 3.3.x


      The quest to get this done has led me to the javascript that is  behind the rich:suggestionBox (@see http://anonsvn.jboss.org/repos/richfaces/tags/3.3.3.Final/ui/suggestionbox/src/main/resources/org/richfaces/renderkit/html/scripts/suggestionbox.js). The methods in question are markPrevious() and markNext().


      This is how they are defined on the Suggestion.Base.prototype :


      {code}markPrevious: function() {
          if (this.index > 0) this.index--;
          //else this.index = this.entryCount - 1;

      markNext: function() {
          if (this.index < this.entryCount - 1) this.index++;
          //else this.index = 0;

      As you can see, the functionality that I need is there, but for some  reason it has been commented out. So I tried to override the methods by  placing the following bit of javascript in my template file that is used  by all my pages:



      {code:xml}<script type="text/javascript">

          if(Suggestion) {
              function newMarkPrevious() {
                  if (this.index > 0) this.index--;
                  else this.index = this.entryCount - 1;

              function newMarkNext() {
                  if (this.index < this.entryCount - 1) this.index++;
                  else this.index = 0;

              Suggestion.Base.prototype.markPrevious = newMarkPrevious;
              Suggestion.Base.prototype.markNext = newMarkNext;


      Now, if I inspect the Suggestion object with firebug, I can  see that the methods indeed get overridden. However, all  rich:suggestionBoxes on my pages still use the old implementation. So,  I'm thinking that somehow, the objects behind the rich:suggestionBoxes  get created before I override the prototype. And this  is where I'm stuck. I don't know how I could get my version in there before any of those suggestionBoxes get created.


      Has anyone got an idea on how to solve this?





      P.S. I realise that there is also the option of just adjusting the  code directly in the richfaces-ui.jar, but I don't want to have a custom built jar.