Skip to content

Instantly share code, notes, and snippets.

@mbriggs
Created April 11, 2013 14:53
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 mbriggs/5364035 to your computer and use it in GitHub Desktop.
Save mbriggs/5364035 to your computer and use it in GitHub Desktop.
marionettte tabbed-view container
var PrescriptionActions = TabbedView.extend({
tabs: {
"Prescription Info": DetailsView,
"Refill": RefillView,
"Renew": DetailsView
}
});
"use strict";
var ItemView = Marionette.ItemView;
var TabbedView = ItemView.extend({
className: 'tab-container',
template: 'tabbed-view',
tabs: {},
ui: {
content: '.content'
},
constructor: function(options){
this.processTabs();
this.buildEvents();
this.events = _.extend(this.tabbedEvents, this.events);
this.args = options;
var args = [].slice.call(arguments);
ItemView.prototype.constructor.call(this, args);
},
processTabs: function(){
this.configs = _.map(this.tabs, function(view, title){
return {
name: title.replace(/ /g, '-').toLowerCase(),
title: title,
view: view
}
});
},
buildEvents: function(){
var container = this;
this.tabbedEvents = {};
this.configs.each(function(config){
var event = 'click .' + config.name;
container.addTabEvent(event, config);
});
},
addTabEvent: function(event, config){
var container = this;
console.log('adding', event)
this.tabbedEvents[event] = function(e){
if(e) e.preventDefault();
var view = new config.view(container.args);
container.attach(view);
container.$('.active').removeClass('active');
container.$('.' + config.name).addClass('active');
}
},
attach: function(view){
var current = this.content.currentView;
var container = this;
if(current) this.stopListening(current);
this.listenTo(view, 'all', function(event){
var args = [].slice.call(arguments);
container.trigger.apply(container, args);
});
this.content.show(view);
},
render: function(){
ItemView.prototype.render.call(this);
this.content = new Marionette.Region({ el: this.ui.content });
this.$('li:first').addClass('active');
var View = this.configs.first().view;
this.attach(new View(this.args));
return this;
},
serializeData: function(){
return this.configs;
}
});
module.exports = TabbedView;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment