Izzy Izzy - 1 year ago 69
jQuery Question

Following URLS in backbone.js on div click

I have started to get familiar with backbone.js and needed a little time to wrap my head around routers, models and views.

Handling routes through router and defining them are piece of cake, until that particular route is defined as

<a href="#some-url">
(I am not sure 100%, but I presume backbone is overriding default link behavior and instead redirecting it dynamically load templates staying on current page).

What I need is acton that is executed on user clicking on div element. It was easy to add event under view, and clicking on the div correctly called the function.

But from that point I was uncertain what to do. I can easily add:
window.location.href = "#some-url"
, and browser will redirect page to asked href, but that seems as breaking one-pager rules backbone is trying to create.

Is there a more appropriate way to handle views change than forcing browser to change href via window.location ?

EDIT: added the code.


require(['jquery', 'backbone', 'app/router'], function ($, Backbone, Router) {
window.router = new Router();


var $ = require('jquery'),
Backbone = require('backbone'),
HomeView = require('app/views/Home'),
$body = $('body'),
homeView = new HomeView({el: $body});
return Backbone.Router.extend({

routes: {
"": "home",
"about": "About"

home: function () {

About: function () {
require(["app/views/About"], function (AboutView) {
var view = new AboutView({el: $body});



return Backbone.View.extend({

events: {
"click #about": "followAbout"
render: function () {
return this;
followAbout: function () {
window.router.navigate( "#about", { trigger: true } );
//window.location.href = "#about";

Answer Source

Lets say you've defined your router like this

 var Workspace = Backbone.Router.extend({
   routes: {
     "help":                 "help",    // #help
     "search/:query":        "search",  // #search/kiwis
     "search/:query/p:page": "search"   // #search/kiwis/p7
   help: function() {
   search: function(query, page) {

And then created a new instance of your router

router = new Workspace();

In your div you can optionally define your route in a data attribute, which will be convenient for you like this

<div id="my_div" data-path="some-url">

When the user will click the div, you take the route from its data and use navigate function to go to that route like this.

$("#my_div").on('click', function(e){
  var path = $(this).attr("data-path");
  router.navigate(path, { trigger: true});
  //this will navigate to the path like a single page app without reloading the page

Checkout backbone docs for more.


//events inside first view
'click #div-1': 'myFunction'

//myfunction code
myFunction: function(e){
  clicked_div = e.currentTarget;
  path = $(clicked_div).attr('data-path'); //I'm assuming you've stroed the path in your div, or you can get it how ever you like

  window.router.navigate(path, {trigger: true});
  //Also I am assuming you've defined your router under the window object directly, so it can be accessed directly. Or you can always namespace it in an object
  //, to avoid collision
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download