Backbone Event Handler

In this example below you will see how to do a Backbone Event Handler with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by jagretz, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright jagretz ©
  • HTML
  • CSS
  • JavaScript
    <body class="clickable">
</body>

/*Downloaded from https://www.codeseek.co/jagretz/backbone-event-handler-gLvGqP */
    body {
  background-color: #eee;
}


/*Downloaded from https://www.codeseek.co/jagretz/backbone-event-handler-gLvGqP */
    var DateView = Backbone.View.extend({
  initialize: function () {
    this.model.on('change', function() {
      this.render();
    }, this);
  },
  events: {
    'click .clickable': 'clickHandler'
  },
  render: function() {
    this.$el.html(this.model.get('fullDate').toString());
    return this;
  },
  // custom functions
  clickHandler: function() {
    console.log('click event fired!');
    alert('smoking');
  }
}, {
  type: 'DateView'
});

var date = new Backbone.Model({
  fullDate: new Date()
});

var dateView = new DateView({model: date, el: 'body'});
dateView.render();

setInterval(function() {date.set('fullDate', new Date())}, 1000);





Comments