Skip to content

Instantly share code, notes, and snippets.

@wfortin
Last active May 31, 2016 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wfortin/2a00af8eb1f759ad8bb16bf9fdf9f915 to your computer and use it in GitHub Desktop.
Save wfortin/2a00af8eb1f759ad8bb16bf9fdf9f915 to your computer and use it in GitHub Desktop.

Disable history feature

data-enable-history=“false"

Manually update the state in the URL hash

var search = $('#search');
search.on('state:change', function() {
  var attributes = search.coveo(Coveo.QueryStateModel).getAttributes();
  var queryString = encodeURIComponent(Coveo.HashUtils.encodeValues(attributes));
  
  // Update the hash
  router.navigate('search?' + queryString); // window.location.hash = 'search?' + queryString
});

Manually parse the hash

var MyRouter = Backbone.Router.extend({
  routes: {
    "search/?:query": "search"
  },


  search: function(query) {
    var newState = this.parseState(query);

    var search = $('#search');
    var currentState = search.coveo('state');
    search.coveo('state', _.defaults(newState, currentState.defaultAttributes));
    search.coveo('executeQuery'); // Not sure if needed everytime
  }
  
  parseState: function() {
    var state = URI.parseQuery(decodeURIComponent(hash));

    var parsedState = {};
    _.each(state, (value, key) => {
      parsedState[key] = Coveo.HashUtils.getValue(key, '#' + decodeURIComponent(hash));
    });

    return parsedState;
  }
});

Should be similar with react-router or angular

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment