Created
April 10, 2013 23:55
-
-
Save fritz-gerneth/5359500 to your computer and use it in GitHub Desktop.
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
(function (Controller, _) { | |
"use strict"; | |
Controller.BasicController = function (parameters) { | |
var paramKeys = _.keys(parameters); | |
this.canDispatch = function (match) { | |
return _.every(paramKeys, function (c) { | |
return parameters[c] === match.getParam(c); | |
}); | |
}; | |
this.destroy = function () {}; | |
}; | |
}(Controller, _)); |
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
(function (Controller, Deps, _) { | |
"use strict"; | |
Controller.Dispatcher = function (matchingSource, controllers) { | |
var availableControllers = controllers || [], | |
controllerDependants = new Deps.Dependency(), | |
currentController; | |
self.addController = function (controller) { | |
availableControllers.push(controller); | |
controllerDependants.changed(); | |
}; | |
Deps.autorun(function () { | |
var match = matchingSource(), | |
oldController = currentController; | |
controllerDependants.depend(); | |
currentController = _.find(availableControllers, function (controller) { | |
return controller.canDispatch(match); | |
}); | |
if (undefined !== currentController) { | |
currentController.dispatch(match); | |
} | |
if (oldController !== currentController && oldController !== undefined) { | |
oldController.destroy(); | |
} | |
}); | |
}; | |
} (Controller, Deps, _)); |
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 view = new Controller.View(Template['account.layout'], Template['account.index']); | |
var AccountIndexAction = function (parameters, view) { | |
Controller.BasicController.call(this, parameters); | |
this.dispatch = function (match) { | |
view.set( | |
Template['account.layout'], | |
Template['account.index'] | |
); | |
}; | |
}; | |
function AccountIndexSurrogateConstructor() {} | |
AccountIndexSurrogateConstructor.prototype = Controller.BasicController.prototype; | |
AccountIndexAction.prototype = new AccountIndexSurrogateConstructor(); | |
AccountIndexAction.prototype.constructor = AccountIndexAction; | |
var c1 = new AccountIndexAction({module: 'account', action: 'index'}, view); | |
var c2 = { | |
canDispatch: function (match) { | |
return 'projects' === match.getParam('action') | |
&& 'account' === match.getParam('module'); | |
}, | |
dispatch: function (match) { | |
view.set(Template['project.layout'], Template['projects.account.list']); | |
}, | |
destroy: function () {} | |
}; | |
var routeList = new Router.SimpleRouteList([ | |
new Router.Routes.Segment('/:module[/:action]', {module: 'index', action: 'index'}) | |
]); | |
var reactiveRouter = new Router.Reactive(routeList, Meteor.Location.getPath); | |
var dispatcher = new Controller.Dispatcher(reactiveRouter.match, [c1, c2]); |
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
(function (Controller) { | |
"use strict"; | |
Controller.View = function (defaultLayout, defaultView) { | |
var self = this, | |
currentLayout = defaultLayout, | |
currentView = defaultView, | |
dependants = new Deps.Dependency(); | |
Handlebars.registerHelper('currentLayout', function () { | |
return new Handlebars.SafeString(self.getLayout()()); | |
}); | |
Handlebars.registerHelper('currentView', function () { | |
return new Handlebars.SafeString(self.getView()()); | |
}); | |
self.getLayout = function () { | |
dependants.depend(); | |
return currentLayout; | |
}; | |
self.setLayout = function (layout) { | |
currentLayout = layout; | |
dependants.changed(); | |
}; | |
self.getView = function () { | |
dependants.depend(); | |
return currentView; | |
}; | |
self.setView = function (view) { | |
currentView = view; | |
dependants.changed(); | |
}; | |
self.set = function (layout, view) { | |
currentLayout = layout; | |
currentView = view; | |
dependants.changed(); | |
}; | |
}; | |
}(Controller)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment