Created
January 21, 2014 12:51
-
-
Save boo1ean/8539441 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| define(["marionette", "underscore"], function(Marionette, _) { | |
| return Marionette.Controller.extend({ | |
| mixins: [], | |
| initialize: function(options) { | |
| this.mixinModules(); | |
| this.m = {}; // Module models | |
| this.c = {}; // Module collections | |
| this.v = {}; // Modules views | |
| this.p = {}; // Module promises | |
| this.options = options; | |
| this.bindActions(); | |
| this.setup(); | |
| this.setupEvents(); | |
| }, | |
| mixinModules: function() { | |
| var that = this; | |
| _.each(this.mixins, function(module) { | |
| // Extend setup methods | |
| var setup = this.setup; | |
| this.setup = function() { | |
| module.prototype.setup.apply(that, arguments); | |
| setup.apply(that, arguments); | |
| } | |
| var setupEvents = this.setupEvents; | |
| this.setupEvents = function() { | |
| module.prototype.setupEvents.apply(that, arguments); | |
| setupEvents.apply(that, arguments); | |
| } | |
| // Copy actions and routes | |
| this.actions = _.extend(this.actions, module.prototype.actions); | |
| this.appRoutes = _.extend(this.appRoutes, module.prototype.appRoutes); | |
| // Copy services methods | |
| _.each(module.prototype, function(prop, name) { | |
| if (_.isFunction(prop) && -1 == _.indexOf(["constructor", "setup", "setupEvents"], name)) { | |
| that[name] = prop; | |
| } | |
| }); | |
| }, this); | |
| }, | |
| bindActions: function() { | |
| var that = this; | |
| var beforeAction = function(piped) { | |
| that.beforeAction(); | |
| return piped; | |
| } | |
| var afterAction = function(piped) { | |
| // Set global module context | |
| app.setContext(that.name); | |
| that.afterAction(); | |
| // Mark links to current location as active | |
| $(".nav-active").removeClass("nav-active"); | |
| $("[href='" + location.hash + "']").addClass("nav-active"); | |
| return piped; | |
| } | |
| for (var i in this.actions) { | |
| this.actions[i] = _.compose(afterAction, _.bind(this.actions[i], this), beforeAction); | |
| } | |
| }, | |
| fromCurrentContext: function() { | |
| return this.name === app.getContext(); | |
| }, | |
| // Initialize collections and stuff | |
| setup: function() {}, | |
| // Bind some awesome event handlers to stuff | |
| setupEvents: function() {}, | |
| // Module actions | |
| actions: {}, | |
| // Before action hook | |
| beforeAction: function() {}, | |
| // After action hook | |
| afterAction: function() {}, | |
| // Will be delegated to the main router | |
| appRoutes: {} | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment