PrimeFaces Ajax Navigation with Browser History/Hashtag

前端 未结 2 786
被撕碎了的回忆
被撕碎了的回忆 2021-01-01 07:25

I have implemented a web application which is a one-page-design. Which basically loads a single page then updates with AJAX the central content. The code is the following:

2条回答
  •  礼貌的吻别
    2021-01-01 08:01

    I have created a blog post explaining how to get this to work using jQuery BBQ, based on your question.

    With jQuery BBQ you can keep track of state, history and allow bookmarking while dynamically modifying the page via AJAX and/or DHTML.. just click the links, use your browser's back and next buttons, reload the page..

    First we should include jQuery BBQ.

     
    

    Now consider we have the menu (with all our nav rules)

      
         
         
          
          
      
    

    Then the centered content

     
    
         
            
         
    
      
    

    the include reflects the currentNav clicked.

    now define a remoteCommand inside the form

    
    

    This remoteCommand will update our currentNav based on the hashtag.

    Create your JS file or include the following code into the document ready

    $(document).ready(function() {
    
       $('.ui-menuitem-link').click(function(event) {
           event.preventDefault();
           var currentNav = $(this).attr('href').
                            substr($(this).attr('href').indexOf("/faces") + 6)
           window.location.hash = '#' + currentNav;
    
       })
    
       $(window).bind('hashchange', function(e) {
    
        var url = $.param.fragment();
        updateNav([{name: 'currentNav', value: url}]); //remoteCommand Call
    
       })
    
       $(window).trigger('hashchange');
    
     });
    

    Basically first we handle our clicks on the menu items, setting the hash of the window.

    then we define an event of the hashchange of the window, using the help of jQuery BBQ.

    ManagedBean

    public void updateNav() {
        FacesContext context = FacesContext.getCurrentInstance();
        Map map = context.getExternalContext().getRequestParameterMap();
        currentNav = (String) map.get("currentNav");
    }
    

    For a complete code, please see my newly post created for the question.

    Primefaces Hash Navigation Using jQuery BBQ

    And also the example is available on github.

    Hope it helps.

提交回复
热议问题