Skip to content

Instantly share code, notes, and snippets.

@jubstuff
Last active August 29, 2015 14:22
Show Gist options
  • Save jubstuff/17f8250b491fa2e7122e to your computer and use it in GitHub Desktop.
Save jubstuff/17f8250b491fa2e7122e to your computer and use it in GitHub Desktop.
Simple Toggle widget with jQuery or Backbone
var toggleFacet = {
init: function (options, elem) {
this.options = $.extend({}, this.options, options);
this.$elem = $(elem);
this.eventify();
},
options: {
toggleClass: 'facet-collapsed'
},
eventify: function () {
var that = this;
this.$elem.on('click', function (e) {
if(e.target !== this) {
return;
}
e.preventDefault();
$(this).toggleClass(that.options.toggleClass);
});
}
};
(function ($) {
$.fn.extend({
toggleFacet: function (options) {
return this.each(function () {
var myToggleFacet = Object.create(toggleFacet);
myToggleFacet.init(options, this);
$(this).data('toggleFacet', myToggleFacet);
});
}
});
})(jQuery);
var ToggleFacet = Backbone.View.extend({
el: '#caratteristiche',
events: {
'click': 'toggleFacet'
},
toggleFacet: function(event) {
event.preventDefault();
this.$el.toggleClass('facet-collapsed');
}
});
$('#caratteristiche, #caratteristiche1, #caratteristiche2').on('click', 'a', function(event){
if(event.target !== this) {
return;
}
event.preventDefault();
$(this).parent().toggleClass('facet-collapsed');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment