1 define(['libs/jquery-1.9.1.min', 'libs/underscore-min'], function($, _) {
\r
3 var Runner = function(app, modules) {
\r
5 function getModuleInstance(moduleName) {
\r
6 var module = moduleInstances[moduleName] = (moduleInstances[moduleName] || modules[moduleName](new Sandbox(moduleName)));
\r
10 var bootstrappedData = {},
\r
12 moduleInstances = {},
\r
13 eventListeners = [];
\r
15 _.each(_.keys(modules || {}), function(moduleName) {
\r
16 if(_.contains(app.permissions[moduleName] || [], 'handleEvents')) {
\r
17 eventListeners.push(moduleName);
\r
23 var Sandbox = function(moduleName) {
\r
27 this.getBootstrappedData = function() {
\r
28 return bootstrappedData[moduleName];
\r
31 this.getTemplate = function(templateName) {
\r
32 return _.template($('[data-template-name="' + moduleName + '.' + templateName + '"]').html().trim());
\r
35 this.publish = function(eventName) {
\r
36 console.log(moduleName + ': ' + eventName);
\r
37 var eventArgs = Array.prototype.slice.call(arguments, 1);
\r
38 _.each(eventListeners, function(listenerModuleName) {
\r
39 var listener = moduleInstances[listenerModuleName];
\r
41 listener.handleEvent(moduleName, eventName, eventArgs);
\r
46 var permissions = app.permissions[moduleName];
\r
48 this.getModule = _.contains(permissions, 'getModule') ? function(requestedModuleName) {
\r
49 return getModuleInstance(requestedModuleName);
\r
52 this.getDOM = _.contains(permissions, 'getDOM') ? function() {
\r
53 return $(options.rootSelector);
\r
59 this.setBootstrappedData = function(moduleName, data) {
\r
60 bootstrappedData[moduleName] = data;
\r
63 this.start = function(_options) {
\r
64 options = _.extend({
\r
65 rootSelector: 'body'
\r
67 app.initModules.forEach(function(moduleName) {
\r
68 getModuleInstance(moduleName).start();
\r