No results found. Try again with different words?

Search must be at least 3 characters.

Listening for Javscript Events

SureFeedback doesn’t necessarily broadcast javascript events on a global level, but the awesome part about Backbone.js is you can listen for DOM or model events by extending views and models.

Model Events

Model events can be listened to using the backbone’s listenTo method. For example, when a model attribute changes, we can listen for the change and run our own functions on the event. Here’s an example of extending a model and running our own function on the event.

Websites:

ph.api.views.CommentBubble = ph.api.views.CommentBubble.extend({
     initialize: function () {
          // we're extending the original method
          this.constructor.__super__.initialize.apply(this, arguments);

	 // output model to console
         console.log(this.model);

          // listen to models comments
          this.listenTo(this.model.get('comments'), 'add', this.prefixDoSomething);
     },
     prefixDoSomething: function(updatedModel){
          // log changed comments
          console.log(this.model.get('comments'));
    }
});

Mockups:

Huddle.CommentView = Huddle.CommentView.extend({
     initialize: function () {
          // we're extending the original method
          this.constructor.__super__.initialize.apply(this, arguments);

	 // output model to console
         console.log(this.model);

          // listen to models comments
          this.listenTo(this.model.items, 'add', this.prefixDoSomething);
     },
     prefixDoSomething: function(updatedModel){
          // log changed comments
          console.log(this.model.items);
    }
});

View Events

View events can be listened to by extending backbone’s events hash. For example, when a something is clicked on a particular view or subview, we can extend the events hash to run our own functions on the event. 

It’s recommended that you use Backbone events hash instead of javascript or jquery events. This is because they are pre-scoped and will automatically remove themselves when the view is removed, ensuring no memory leaks and other performance issues!

Here’s an example of extending the events hash and running our own function on the event.

Websites

ph.api.views.Comment = ph.api.views.Comment.extend({
     // Add new events
     events: function () {
          // we're extending the original events here with our own
          return _.extend({}, this.constructor.__super__.events, {
               'hover .ph-annotation-dot': 'prefixDoSomething'
          });
     },

     // add our own method
     prefixDoSomething: function () {
          console.log('hovered!');
     }
});

Mockups

Huddle.ImageView = Huddle.ImageView.extend({
	// Add new events
	events: function () {
		// we're extending the original events here with our own
		return _.extend({}, this.constructor.__super__.events, {
			'hover img.ph-project-image': 'prefixDoSomething'
		});
	},

	// add our own method
	prefixDoSomething: function () {
		console.log('hovered!');
	}
});

Was this article helpful?


Did not find a solution? We are here to help you succeed.

Listening for Javscript Events

SureFeedback doesn’t necessarily broadcast javascript events on a global level, but the awesome part about Backbone.js is you can listen for DOM or model events by extending views and models.

Model Events

Model events can be listened to using the backbone’s listenTo method. For example, when a model attribute changes, we can listen for the change and run our own functions on the event. Here’s an example of extending a model and running our own function on the event.

Websites:

ph.api.views.CommentBubble = ph.api.views.CommentBubble.extend({
     initialize: function () {
          // we're extending the original method
          this.constructor.__super__.initialize.apply(this, arguments);

	 // output model to console
         console.log(this.model);

          // listen to models comments
          this.listenTo(this.model.get('comments'), 'add', this.prefixDoSomething);
     },
     prefixDoSomething: function(updatedModel){
          // log changed comments
          console.log(this.model.get('comments'));
    }
});

Mockups:

Huddle.CommentView = Huddle.CommentView.extend({
     initialize: function () {
          // we're extending the original method
          this.constructor.__super__.initialize.apply(this, arguments);

	 // output model to console
         console.log(this.model);

          // listen to models comments
          this.listenTo(this.model.items, 'add', this.prefixDoSomething);
     },
     prefixDoSomething: function(updatedModel){
          // log changed comments
          console.log(this.model.items);
    }
});

View Events

View events can be listened to by extending backbone’s events hash. For example, when a something is clicked on a particular view or subview, we can extend the events hash to run our own functions on the event. 

It’s recommended that you use Backbone events hash instead of javascript or jquery events. This is because they are pre-scoped and will automatically remove themselves when the view is removed, ensuring no memory leaks and other performance issues!

Here’s an example of extending the events hash and running our own function on the event.

Websites

ph.api.views.Comment = ph.api.views.Comment.extend({
     // Add new events
     events: function () {
          // we're extending the original events here with our own
          return _.extend({}, this.constructor.__super__.events, {
               'hover .ph-annotation-dot': 'prefixDoSomething'
          });
     },

     // add our own method
     prefixDoSomething: function () {
          console.log('hovered!');
     }
});

Mockups

Huddle.ImageView = Huddle.ImageView.extend({
	// Add new events
	events: function () {
		// we're extending the original events here with our own
		return _.extend({}, this.constructor.__super__.events, {
			'hover img.ph-project-image': 'prefixDoSomething'
		});
	},

	// add our own method
	prefixDoSomething: function () {
		console.log('hovered!');
	}
});

Leave a Reply

Your email address will not be published. Required fields are marked *

LET’S GET STARTED

Ready to Give It a Try?

Start Your Free 14-Day Trial Now. No Obligation. No Reason Not To.

Trial Icon

14 Days Free Trial

Experience Our Platform Risk-Free

Docs Icon

Documentation

Articles that cover common questions

24/7 World Class Support Team

Friendly Support

Reach Out – We’re Here to Help

Scroll to Top