Support for Raw Handlebars and helpers in Ember 1.13

This commit is contained in:
Robin Ward 2016-10-25 17:53:03 -04:00
parent 41f19641d1
commit b601120b39
7 changed files with 51 additions and 20 deletions

View File

@ -55,12 +55,6 @@ 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")) {
@ -70,6 +64,14 @@ export function registerUnbound(name, fn) {
return fn.call(this, property, resolveParams(this, options));
};
if (Ember.Helper) {
_helpers[name] = Ember.Helper.extend({
compute: (params, args) => fn(params[0], args)
});
Handlebars.registerHelper(name, func);
return;
}
Handlebars.registerHelper(name, func);
Ember.Handlebars.registerHelper(name, func);
}

View File

@ -132,6 +132,12 @@ export function buildResolver(baseName) {
},
resolveTemplate(parsedName) {
let fullName = parsedName.fullNameWithoutType;
if (fullName.indexOf('.raw') !== -1) {
return Ember.TEMPLATES[fullName] ||
Ember.TEMPLATES[fullName.replace(/\.raw$/, '')];
}
return this.findPluginMobileTemplate(parsedName) ||
this.findPluginTemplate(parsedName) ||
this.findMobileTemplate(parsedName) ||

View File

@ -1,17 +1,27 @@
import { registerUnbound } from 'discourse-common/lib/helpers';
// see: https://github.com/emberjs/ember.js/issues/12634
var missingViews = {};
let _injections;
function renderRaw(ctx, template, templateName, params) {
function renderRaw(ctx, container, template, templateName, params) {
params.parent = params.parent || ctx;
if (!params.view && !missingViews[templateName]) {
var viewClass = Discourse.__container__.lookupFactory('view:' + templateName);
if (viewClass) {
params.view = viewClass.create(params);
} else {
missingViews[templateName] = true;
if (!params.view) {
if (!_injections) {
_injections = {
siteSettings: container.lookup('site-settings:main'),
currentUser: container.lookup('currentUser:main'),
site: container.lookup('site:main'),
session: container.lookup('session:main'),
topicTrackingState: container.lookup('topic-tracking-state:main')
};
}
const module = `discourse/views/${templateName}`;
if (requirejs.entries[module]) {
const viewClass = require(module, null, null, true);
if (viewClass && viewClass.default) {
params.view = viewClass.default.create(params, _injections);
}
}
}
@ -19,10 +29,13 @@ function renderRaw(ctx, template, templateName, params) {
}
registerUnbound('raw', function(templateName, params) {
var template = Discourse.__container__.lookup('template:' + templateName + '.raw');
templateName = templateName.replace('.', '/');
const container = Discourse.__container__;
var template = container.lookup('template:' + templateName + '.raw');
if (!template) {
Ember.warn('Could not find raw template: ' + templateName);
return;
}
return renderRaw(this, template, templateName, params);
return renderRaw(this, container, template, templateName, params);
});

View File

@ -1,4 +1,3 @@
{{site-header canSignUp=canSignUp
showCreateAccount="showCreateAccount"
showLogin="showLogin"

View File

@ -1 +1 @@
<{{this.tagName}} class="{{class}} {{cold-age-class topic.createdAt startDate=topic.bumpedAt class=""}} activity" title="{{{topic.bumpedAtTitle}}}"><a href="{{topic.lastPostUrl}}">{{format-date topic.bumpedAt format="tiny" noTitle="true"}}</a></{{this.tagName}}>
<{{tagName}} class="{{class}} {{cold-age-class topic.createdAt startDate=topic.bumpedAt class=""}} activity" title="{{{topic.bumpedAtTitle}}}"><a href="{{topic.lastPostUrl}}">{{format-date topic.bumpedAt format="tiny" noTitle="true"}}</a></{{tagName}}>

View File

@ -1,5 +1,4 @@
export default Ember.Object.extend({
localizedName: function(){
if(this.forceName){
return this.forceName;

View File

@ -54,6 +54,18 @@ end
class Ember::Handlebars::Template
include Discourse::Ember::Handlebars::Helper
def precompile_handlebars(string, input=nil)
"require('discourse-common/lib/raw-handlebars').template(#{Barber::Precompiler.compile(string)});"
end
def compile_handlebars(string, input=nil)
"require('discourse-common/lib/raw-handlebars').compile(#{indent(string).inspect});"
end
def global_template_target(namespace, module_name, config)
"#{namespace}[#{template_path(module_name, config).inspect}]"
end
# FIXME: Previously, ember-handlebars-templates uses the logical path which incorrectly
# returned paths with the `.raw` extension and our code is depending on the `.raw`
# to find the right template to use.