Skip to content

Instantly share code, notes, and snippets.

@NahimNasser
Created November 13, 2012 20:11
Show Gist options
  • Save NahimNasser/4068097 to your computer and use it in GitHub Desktop.
Save NahimNasser/4068097 to your computer and use it in GitHub Desktop.
Javascript Backbone Marionette Truncation View
View.TruncationView = Backbone.Marionette.View.extend({
limit: 3,
events: {
'click .btn_less': 'truncateClicked',
'click .btn_more': 'unTruncateClicked'
},
truncateClicked: function(e){
e.preventDefault();
this.truncate();
},
unTruncateClicked: function(e){
e.preventDefault();
this.unTruncate();
},
truncate: function() {
var index = this.limit - 1;
var top = this.$el.find('li').eq( index ).position().top;
var height = this.$el.find('li').eq( index ).outerHeight();
this.$el.find('ul').height( top + height );
this.$el.find('.btn_less').hide();
this.$el.find('.btn_more').show();
},
unTruncate: function() {
this.$el.find('ul').css('height', 'auto');
this.$el.find('.btn_less').show();
this.$el.find('.btn_more').hide();
},
onFetched: function() {
if ( this.$el.find('li').length > this.limit ) {
this.truncate();
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment