Created
April 11, 2013 14:53
-
-
Save mbriggs/5364035 to your computer and use it in GitHub Desktop.
marionettte tabbed-view container
This file contains 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
var PrescriptionActions = TabbedView.extend({ | |
tabs: { | |
"Prescription Info": DetailsView, | |
"Refill": RefillView, | |
"Renew": DetailsView | |
} | |
}); |
This file contains 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
"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