Created
October 27, 2013 02:34
-
-
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 …
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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