1 define(['libs/jquery', 'libs/underscore'], function($, _) {
5 var Runner = function(app, modules) {
7 function getModuleInstance(moduleName) {
8 var module = moduleInstances[moduleName] = (moduleInstances[moduleName] || modules[moduleName](new Sandbox(moduleName)));
12 var bootstrappedData = {},
18 _.each(_.keys(modules || {}), function(moduleName) {
19 if(_.contains(app.permissions[moduleName] || [], 'handleEvents')) {
20 eventListeners.push(moduleName);
26 var Sandbox = function(moduleName) {
30 this.getBootstrappedData = function() {
31 return bootstrappedData[moduleName];
34 this.getTemplate = function(templateName) {
35 return _.template($('[data-template-name="' + moduleName + '.' + templateName + '"]').html().trim());
38 this.publish = function(eventName) {
39 var eventArgs = Array.prototype.slice.call(arguments, 1);
40 _.each(eventListeners, function(listenerModuleName) {
41 var listener = moduleInstances[listenerModuleName];
43 listener.handleEvent(moduleName, eventName, eventArgs);
48 var permissions = app.permissions[moduleName];
50 this.getModule = _.contains(permissions, 'getModule') ? function(requestedModuleName) {
51 return getModuleInstance(requestedModuleName);
54 this.getDOM = _.contains(permissions, 'getDOM') ? function() {
55 return $(options.rootSelector);
58 this.getPlugins = function() {
64 this.setBootstrappedData = function(moduleName, data) {
65 bootstrappedData[moduleName] = data;
68 this.registerPlugin = function(plugin) {
72 this.start = function(_options) {
76 app.initModules.forEach(function(moduleName) {
77 getModuleInstance(moduleName).start();