Max SchmittMS
27th August 2013

jQuery Component for Crafty

I'm currently working on a little puzzle-game called pozzle, which uses the Crafty engine. Today I'm working on cleaning up my UI-code.

Here is a little jQuery component, that makes a jQuery-wrapper of the entity's DOM-element available. You call it by using this.$element, where this refers to your entity with the JQuery component attached.

Crafty.c('JQuery', {
init: function() {
this.requires('DOM')
this._rebindJQuerySelector()
this.bind('Change', this._rebindJQuerySelector)
},
remove: function() {
this.unbind('Change', this._rebindJQuerySelector)
},
_rebindJQuerySelector: function() {
this.$element = $(this._element)
}
})