From bd352385a5f135b67c9220d8b6cd5b55ee37ca48 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Mon, 24 Oct 2016 13:49:17 -0400 Subject: [PATCH] Framework for supporting Ember 1.12/1.13 helpers simulataneously --- .../discourse-common/lib/helpers.js.es6 | 25 ++++++++++++++++++- .../discourse-common/resolver.js.es6 | 7 ++++-- .../discourse/helpers/custom-html.js.es6 | 12 ++------- .../discourse/helpers/plugin-outlet.js.es6 | 18 ------------- .../initializers/auto-load-modules.js.es6 | 5 +++- .../wizard/initializers/load-helpers.js.es6 | 5 +++- 6 files changed, 39 insertions(+), 33 deletions(-) diff --git a/app/assets/javascripts/discourse-common/lib/helpers.js.es6 b/app/assets/javascripts/discourse-common/lib/helpers.js.es6 index 45c065124cd..470ae264fed 100644 --- a/app/assets/javascripts/discourse-common/lib/helpers.js.es6 +++ b/app/assets/javascripts/discourse-common/lib/helpers.js.es6 @@ -13,8 +13,24 @@ export function htmlHelper(fn) { } } +const _helpers = {}; + export function registerHelper(name, fn) { - Ember.HTMLBars._registerHelper(name, fn); + if (Ember.Helper) { + _helpers[name] = Ember.Helper.helper(fn); + } else { + return Ember.HTMLBars._registerHelper(name, fn); + } +} + +export function findHelper(name) { + return _helpers[name]; +} + +export function registerHelpers(registry) { + Object.keys(_helpers).forEach(name => { + registry.register(`helper:${name}`, _helpers[name], { singleton: false }); + }); } function resolveParams(ctx, options) { @@ -39,6 +55,13 @@ function resolveParams(ctx, options) { } export function registerUnbound(name, fn) { + if (Ember.Helper) { + _helpers[name] = Ember.Helper.helper(function() { + // TODO: Allow newer ember to use helpers + }); + return; + } + const func = function(property, options) { if (options.types && (options.types[0] === "ID" || options.types[0] === "PathExpression")) { property = get(this, property, options); diff --git a/app/assets/javascripts/discourse-common/resolver.js.es6 b/app/assets/javascripts/discourse-common/resolver.js.es6 index 34222872df7..e874b5a7336 100644 --- a/app/assets/javascripts/discourse-common/resolver.js.es6 +++ b/app/assets/javascripts/discourse-common/resolver.js.es6 @@ -1,5 +1,6 @@ -/* global requirejs, require */ +import { findHelper } from 'discourse-common/lib/helpers'; +/* global requirejs, require */ var classify = Ember.String.classify; var get = Ember.get; @@ -109,7 +110,9 @@ export function buildResolver(baseName) { }, resolveHelper(parsedName) { - return this.customResolve(parsedName) || this._super(parsedName); + return findHelper(parsedName.fullNameWithoutType) || + this.customResolve(parsedName) || + this._super(parsedName); }, resolveController(parsedName) { diff --git a/app/assets/javascripts/discourse/helpers/custom-html.js.es6 b/app/assets/javascripts/discourse/helpers/custom-html.js.es6 index d862cb02fd8..f3749dfc4d9 100644 --- a/app/assets/javascripts/discourse/helpers/custom-html.js.es6 +++ b/app/assets/javascripts/discourse/helpers/custom-html.js.es6 @@ -20,15 +20,7 @@ export function setCustomHTML(key, html) { _customizations[key] = html; } -registerHelper('custom-html', function(params, hash, options, env) { - const name = params[0]; - const html = getCustomHTML(name); +registerHelper('custom-html', function(params) { + const html = getCustomHTML(params[0]); if (html) { return html; } - - const contextString = params[1]; - const target = (env || contextString); - const container = target.container || target.data.view.container; - if (container.lookup('template:' + name)) { - return env.helpers.partial.helperFunction.apply(this, arguments); - } }); diff --git a/app/assets/javascripts/discourse/helpers/plugin-outlet.js.es6 b/app/assets/javascripts/discourse/helpers/plugin-outlet.js.es6 index 69df9206330..4426714bf1c 100644 --- a/app/assets/javascripts/discourse/helpers/plugin-outlet.js.es6 +++ b/app/assets/javascripts/discourse/helpers/plugin-outlet.js.es6 @@ -29,17 +29,6 @@ And it will be wired up automatically. - ## The block form - - If you use the block form of the outlet, its contents will be displayed - if no connectors are found. Example: - - ```handlebars - {{#plugin-outlet "hello-world"}} - Nobody says hello :'( - {{/plugin-outlet}} - ``` - ## Disabling If a plugin returns a disabled status, the outlets will not be wired up for it. @@ -174,13 +163,6 @@ registerHelper('plugin-outlet', function(params, hash, options, env) { }); } } - } else if (options.isBlock) { - const virtualView = Ember.View.extend({ - isVirtual: true, - tagName: hash.tagName || '', - template: options.template - }); - env.helpers.view.helperFunction.call(this, [virtualView], hash, options, env); } }); diff --git a/app/assets/javascripts/discourse/initializers/auto-load-modules.js.es6 b/app/assets/javascripts/discourse/initializers/auto-load-modules.js.es6 index b5bf7cc0415..4920eca574c 100644 --- a/app/assets/javascripts/discourse/initializers/auto-load-modules.js.es6 +++ b/app/assets/javascripts/discourse/initializers/auto-load-modules.js.es6 @@ -1,4 +1,6 @@ -export function autoLoadModules() { +import { registerHelpers } from 'discourse-common/lib/helpers'; + +export function autoLoadModules(container, registry) { Object.keys(requirejs.entries).forEach(entry => { if ((/\/helpers\//).test(entry)) { require(entry, null, null, true); @@ -7,6 +9,7 @@ export function autoLoadModules() { require(entry, null, null, true); } }); + registerHelpers(registry); } export default { diff --git a/app/assets/javascripts/wizard/initializers/load-helpers.js.es6 b/app/assets/javascripts/wizard/initializers/load-helpers.js.es6 index 4b645756a24..8bbbff23d58 100644 --- a/app/assets/javascripts/wizard/initializers/load-helpers.js.es6 +++ b/app/assets/javascripts/wizard/initializers/load-helpers.js.es6 @@ -1,11 +1,14 @@ +import { registerHelpers } from 'discourse-common/lib/helpers'; + export default { name: 'load-helpers', - initialize() { + initialize(container, registry) { Object.keys(requirejs.entries).forEach(entry => { if ((/\/helpers\//).test(entry)) { require(entry, null, null, true); } }); + registerHelpers(registry); } };