Skip to content

Instantly share code, notes, and snippets.

@jvnlwn
Created October 27, 2013 02:34
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 jvnlwn/7177356 to your computer and use it in GitHub Desktop.
Save jvnlwn/7177356 to your computer and use it in GitHub Desktop.
Working on chess.js and ran into an issue when working with Backbone.js listener events inside of views. For my app, every time a chess piece is captured, it is destroyed, added to another collection for captured pieces, and a listener in the view will remove itself when its model is destroyed. When a pawn is promoted, I decided I would destroy …
PieceView = Backbone.View.extend({
initialize: function(options) {
this.options = options;
$('.chess-board').append(this.$el);
var that = this;
this.listenTo(this.model, 'destroy', function() {
that.remove()
})
})
PromotionView = Backbone.View.extend({
events: {
'click': 'promote'
},
initialize: function(options) {
this.options = options;
$('body').append(this.$el);
},
promote: function() {
this.model.destroy()
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment