Tag Archives: ui

How to do toggle functions in Backbone.js the right way

Recently a co-worker was asking me how best to implement a piece of toggle functionality in to a Backbone.js view. He was trying to replicate the usual jQuery toggle pattern within his backbone app but was getting confused and having some troubles accessing it from other views. This confusion comes from the fact that backbone suggests you create all of the view’s events using the events hash.

I’m writing this blog post because I actually had the exact same issue when I first started working with backbone so I feel it is a common issue and hopefully if you are reading this post I can shed some light on the subject for you.

Toggle functionality can be common in UI designs and usually is used to implement some kind of ability to turn something on and off or to disable then enable a piece of the UI.

There is an answer for this question on StackOverflow but personally I don’t really like the answer and would like to show you a more usable and dynamic way of creating some toggle functionality within your backbone views.

I prefer my approach below because I can easily allow other views access to my methods via a global events dispatcher or simply check the state of the object from another view and maybe display a message to the user within another view.

The ability to turn toggle functionality on and off in different places is really neat. This can easily crop up for instance when a client wants to move the button from one place to another and you find your self struggling with the move. Because we have the toggle state included as a property of the object we can easily check the current state of an element if need be, it will allow for much more complex use cases.

var View = Backbone.View.extend({
  events: {
    'click': 'toggleContent'
  },
  showmeState: true,
  toggleContent: function(){
    if (this.showmeState === false) {
      this.showText();
    } else {
      this.hideText();
    }
  },
  hideText: function() {
    this.$el.find('p').hide();
    this.showmeState = false;
  },
  showText: function() {
    this.$el.find('p').show();
    this.showmeState = true;
  }
});

Another use case for the above code would be for an Auto Sorting and Render of a collection in a live data environment.

var View = Backbone.View.extend({
  autoSortTimer: "",
  events: {
    'click': 'autoSort'
  },
  autoSorting: false,
  autoSort: function() {
    if(!this.autoSorting) {
      this.startAutoSort();
    } else {
      this.stopAutoSort();
    }
  },
  startAutoSort: function() {
    this.autoSorting = true;
    this.autoSortTimer = setInterval(function() {
      Dispatcher.trigger('resort');
    }, 1000);
    this.$el.find('#auto-sort-label').html('Auto Sort Columns - On');
  },
  stopAutoSort: function() {
    this.autoSorting = false;
    var autoSortTimer = this.autoSortTimer;
    clearInterval(autoSortTimer);
    this.autoSortTimer = "";
    this.$el.find('#auto-sort-label').html('Auto Sort Columns - Off');
  }
});