Proof of concept: Load controller via ES6 module in Discourse

This commit is contained in:
Robin Ward 2014-04-29 20:59:57 -04:00
parent c21d3f41d0
commit 0327f469c1
8 changed files with 119 additions and 4 deletions

View File

@ -72,5 +72,6 @@
"white": false,
"eqnull": true,
"quotmark": false,
"lastsemic": true
"lastsemic": true,
"esnext": true
}

View File

@ -139,6 +139,7 @@ gem 'rest-client'
gem 'rinku'
gem 'sanitize'
gem 'sass'
gem 'es6_module_transpiler-rails'
gem 'sidekiq'
gem 'sidekiq-failures'

View File

@ -83,6 +83,8 @@ GEM
ember-source (1.6.0.beta.2)
handlebars-source (~> 1.0)
erubis (2.7.0)
es6_module_transpiler-rails (0.4.0)
execjs
eventmachine (1.0.3)
excon (0.28.0)
execjs (2.0.2)
@ -401,6 +403,7 @@ DEPENDENCIES
email_reply_parser-discourse
ember-rails
ember-source (= 1.6.0.beta.2)
es6_module_transpiler-rails
eventmachine
fabrication
fakeweb (~> 1.3.0)

View File

@ -7,7 +7,7 @@
@uses Discourse.ModalFunctionality
@module Discourse
**/
Discourse.HistoryController = Discourse.ObjectController.extend(Discourse.ModalFunctionality, {
export default Discourse.ObjectController.extend(Discourse.ModalFunctionality, {
loading: false,
viewMode: "side_by_side",
revisionsTextKey: "post.revisions.controls.comparing_previous_to_current_out_of_total",

View File

@ -1,3 +1,4 @@
/* global requirejs, require */
/**
A custom resolver to allow template names in the format we like.
@ -8,6 +9,17 @@
**/
Discourse.Resolver = Ember.DefaultResolver.extend({
resolveController: function(parsedName) {
var moduleName = "discourse/controllers/" + parsedName.fullNameWithoutType,
module = requirejs.entries[moduleName];
if (module) {
module = require(moduleName, null, null, true /* force sync */);
if (module && module['default']) { module = module['default']; }
}
return module || this._super(parsedName);
},
/**
Attaches a view and wires up the container properly

View File

@ -5,9 +5,9 @@
//= require jquery_include.js
//= require ember_include.js
//= require loader
//= require message-bus
//= require jquery.ui.widget.js
//= require_tree ./discourse/ember
//= require LAB.js
//= require Markdown.Converter.js
//= require better_markdown.js
@ -38,3 +38,4 @@
//= require lock-on.js
//= require ember-cloaking
//= require break_string
//= require_tree ./discourse/ember

View File

@ -12,6 +12,9 @@ class DiscourseIIFE < Sprockets::Processor
return data if (path =~ /test\_helper\.js/)
return data if (path =~ /javascripts\/helpers\//)
# Ignore ES6 files
return data if (path =~ /\.es6/)
# Ignore translations
return data if (path =~ /\/translations/)
@ -23,4 +26,4 @@ class DiscourseIIFE < Sprockets::Processor
"(function () {\n\nvar $ = window.jQuery;\n// IIFE Wrapped Content Begins:\n\n#{data}\n\n// IIFE Wrapped Content Ends\n\n })(this);"
end
end
end

94
vendor/assets/javascripts/loader.js vendored Normal file
View File

@ -0,0 +1,94 @@
var define, requireModule, require, requirejs;
(function() {
var registry = {}, seen = {}, state = {};
var FAILED = false;
define = function(name, deps, callback) {
registry[name] = {
deps: deps,
callback: callback
};
};
function reify(deps, name, seen) {
var length = deps.length;
var reified = new Array(length);
var dep;
var exports;
for (var i = 0, l = length; i < l; i++) {
dep = deps[i];
if (dep === 'exports') {
exports = reified[i] = seen;
} else {
reified[i] = require(resolve(dep, name));
}
}
return {
deps: reified,
exports: exports
};
}
requirejs = require = requireModule = function(name) {
if (state[name] !== FAILED &&
seen.hasOwnProperty(name)) {
return seen[name];
}
if (!registry[name]) {
throw new Error('Could not find module ' + name);
}
var mod = registry[name];
var reified;
var module;
var loaded = false;
seen[name] = { }; // placeholder for run-time cycles
try {
reified = reify(mod.deps, name, seen[name]);
module = mod.callback.apply(this, reified.deps);
loaded = true;
} finally {
if (!loaded) {
state[name] = FAILED;
}
}
return reified.exports ? seen[name] : (seen[name] = module);
};
function resolve(child, name) {
if (child.charAt(0) !== '.') { return child; }
var parts = child.split('/');
var nameParts = name.split('/');
var parentBase;
if (nameParts.length === 1) {
parentBase = nameParts;
} else {
parentBase = nameParts.slice(0, -1);
}
for (var i = 0, l = parts.length; i < l; i++) {
var part = parts[i];
if (part === '..') { parentBase.pop(); }
else if (part === '.') { continue; }
else { parentBase.push(part); }
}
return parentBase.join('/');
}
requirejs.entries = requirejs._eak_seen = registry;
requirejs.clear = function(){
requirejs.entries = requirejs._eak_seen = registry = {};
seen = state = {};
};
})();