Skip to content

Instantly share code, notes, and snippets.

@fritz-gerneth
Created April 10, 2013 23:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fritz-gerneth/5359500 to your computer and use it in GitHub Desktop.
Save fritz-gerneth/5359500 to your computer and use it in GitHub Desktop.
(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, _));
(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, _));
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]);
(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